Persist workflow entry fields to order

This commit is contained in:
Almira Krdzic
2018-11-05 08:48:25 +01:00
parent 4e8b04fa8d
commit 3dcef84709
9 changed files with 253 additions and 86 deletions

View File

@@ -11,8 +11,192 @@ class Wiaas_Order_Fields {
add_action( 'gform_field_standard_settings', array( __CLASS__, 'field_settings' ) );
add_filter('gform_fileupload_entry_value_file_path', array( __CLASS__, 'display_order_document_fields' ), 999, 2);
add_action('gform_post_update_entry', array(__CLASS__, 'maybe_map_entry_to_order'), 10, 2);
add_action('gform_after_update_entry', array(__CLASS__, 'maybe_apply_user_input_changes'), 10, 3);
}
/**
* Workflow user input are handled separately in gravity flow. We get them here
* and then try to apply changes to order
*
* @param $form
* @param $entry_id
* @param $original_entry
*/
public static function maybe_apply_user_input_changes($form, $entry_id, $original_entry) {
$entry = GFAPI::get_entry($entry_id);
self::maybe_map_entry_to_order($entry, $original_entry);
}
/**
* Persist workflow entry change to order
*
* @param $entry
* @param $old_entry
*/
public static function maybe_map_entry_to_order($entry, $old_entry) {
$form = GFAPI::get_form($old_entry['form_id']);
$order_field = GFCommon::get_fields_by_type($form, 'wiaas_order')[0];
if (empty($order_field)) {
return;
}
$order_id = $entry[$order_field->id];
// get order process entry
$order = wc_get_order($order_id);
$process_entry = Wiaas_Delivery_Process::get_order_delivery_process_entry($order_id);
if ( !$order || empty($process_entry) ) {
return;
}
// apply entry changes to order properties
foreach ($form['fields'] as $field) {
$old_value = $old_entry[$field->id];
$new_value = $entry[$field->id];
// check if field changed
if ($old_value === $new_value || empty($new_value)) {
continue;
}
// save changes to order
switch ($field->type) {
case 'wiaas_order_bundle_document':
/**
* Persist delivery flow documents for bundle
*/
// get corresponding bundle field
$bundle_field = GFCommon::get_fields_by_type( $form, 'wiaas_order_bundle' )[0];
if (empty($bundle_field)) {
continue;
}
$bundle_item = $bundle_field->get_bundle_item( $entry[$bundle_field->id] );
if (empty($bundle_item)) {
continue;
}
$bundle_documents = $bundle_item['wiaas_delivery_documents'];
if (empty($bundle_documents)) {
$bundle_documents = array();
}
$new_documents = $field->multipleFiles ? json_decode( $new_value ) : array( $new_value );
if (! empty($old_value) ) {
$old_documents = $field->multipleFiles ? json_decode( $old_value ) : array( $old_value );
$added_documents = array_diff($new_documents, $old_documents);
} else {
$added_documents = $new_documents;
}
foreach ($added_documents as $added_document) {
$info = pathinfo( $added_document );
$bundle_documents[] = array(
'name' => $info['basename'],
'extension' => $info['extension'],
'url' => $field->get_download_url( $added_document, true ),
'type' => $field->wiaasDocTypeFilter
);
}
$bundle_item->update_meta_data('wiaas_delivery_documents', $bundle_documents);
$bundle_item->save_meta_data();
break;
case 'wiaas_order_document':
/**
* Persist delivery flow documents for order
*/
$new_documents = $field->multipleFiles ? json_decode( $new_value ) : array( $new_value );
if (! empty($old_value) ) {
$old_documents = $field->multipleFiles ? json_decode( $old_value ) : array( $old_value );
$added_documents = array_diff($new_documents, $old_documents);
} else {
$added_documents = $new_documents;
}
$order_documents = $order->get_meta('wiaas_delivery_documents', true);
if (empty($order_documents)) {
$order_documents = array();
}
foreach ($added_documents as $added_document) {
$info = pathinfo( $added_document );
$order_documents[] = array(
'name' => $info['basename'],
'extension' => $info['extension'],
'url' => $field->get_download_url( $added_document, true ),
'type' => $field->wiaasDocTypeFilter
);
}
$order->update_meta_data('wiaas_delivery_documents', $order_documents);
$order->save_meta_data();
break;
case 'wiaas_order_installation_select':
/**
* Persist installation for bundle
*/
$selected_installation = $field->get_selected_installation($new_value);
if (empty($selected_installation)) {
// no installation selected
continue;
}
// get corresponding bundle field
$bundle_field = GFCommon::get_fields_by_type( $form, 'wiaas_order_bundle' )[0];
if (empty($bundle_field)) {
continue;
}
$bundle_item = $bundle_field->get_bundle_item( $entry[$bundle_field->id] );
if (empty($bundle_item)) {
continue;
}
$bundle_item->update_meta_data('wiaas_installation', $selected_installation->get_id());
$bundle_item->save_meta_data();
}
}
}
/**
* Adds the Order Fields group to the form editor.
*