Action
uncanny-automator-pro
uap_wc_save_order_item_meta_by_term
Fires when order item meta is saved based on product terms, integrating with WooCommerce.
add_action( 'uap_wc_save_order_item_meta_by_term', $callback, 10, 1 );
Description
Fires after an order item's metadata is processed when a product in a specific term is ordered. Developers can use this hook to perform custom actions or modify the saved order item meta data. This hook is called within the Uncanny Automator Pro WooCommerce integration.
Usage
add_action( 'uap_wc_save_order_item_meta_by_term', 'your_function_name', 10, 1 );
Parameters
-
$complete_run(mixed) - This parameter is not used in the provided code snippet and its purpose is unclear from the context.
Examples
<?php
/**
* Example function to hook into uap_wc_save_order_item_meta_by_term.
*
* This function demonstrates how to access and potentially modify order item metadata
* when a specific Uncanny Automator trigger related to WooCommerce products in a term
* has completed.
*
* @param array $complete_run An array containing information about the completed trigger run,
* including 'return_args' which holds the results from the trigger.
*/
function my_uncanny_automator_save_order_item_meta( $complete_run ) {
// Check if the necessary data exists in the provided arguments.
if ( ! isset( $complete_run['return_args'] ) || ! is_array( $complete_run['return_args'] ) ) {
return; // Exit if no return arguments are available.
}
$trigger_data = $complete_run['return_args'];
// We expect specific keys from the trigger, let's assume these are available.
// You'll need to inspect the actual $trigger_data structure from Uncanny Automator
// to know the exact keys. For this example, we'll assume 'order_id' and 'product_id'.
$order_id = isset( $trigger_data['order_id'] ) ? absint( $trigger_data['order_id'] ) : 0;
$product_id = isset( $trigger_data['product_id'] ) ? absint( $trigger_data['product_id'] ) : 0;
if ( ! $order_id || ! $product_id ) {
return; // Exit if essential order or product data is missing.
}
$order = wc_get_order( $order_id );
if ( ! $order ) {
return; // Exit if the order cannot be retrieved.
}
// This hook is specifically for saving order item meta by term.
// We might want to add a custom meta field to the relevant order item(s)
// associated with the triggered product.
foreach ( $order->get_items() as $item_id => $item ) {
if ( $item->get_product_id() === $product_id ) {
// Example: Add a custom meta field indicating the trigger processed this item.
// Replace 'my_custom_trigger_processed' with a meaningful meta key.
// Replace 'true' with any relevant data you want to store.
wc_update_order_item_meta( $item_id, 'my_custom_trigger_processed', true );
// You could also update or add other meta fields here based on
// logic derived from $complete_run or other sources.
// For example, if $complete_run contained a specific status:
// $trigger_status = isset($complete_run['trigger_status']) ? sanitize_text_field($complete_run['trigger_status']) : 'completed';
// wc_update_order_item_meta($item_id, '_trigger_status', $trigger_status);
// If you found the item and updated it, you might break if you only
// expect one instance of the product in the order for this trigger.
// break;
}
}
}
// Add the action hook. The `3` indicates the number of arguments our function accepts.
// In this case, the hook passes one argument: $complete_run.
// If the hook passed more arguments, we would list them here as integers.
// For 'uap_wc_save_order_item_meta_by_term', the source code shows it passes only one argument.
add_action( 'uap_wc_save_order_item_meta_by_term', 'my_uncanny_automator_save_order_item_meta', 10, 1 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
uncanny-automator-pro/src/integrations/woocommerce/triggers/anon-product-in-specific-term-has-order-status-changed.php:143
public function wc_order_status_changed( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
if ( ! $order ) {
return;
}
$to_complete = $this->prepare_for_completion( $order );
if ( empty( $to_complete ) ) {
return;
}
foreach ( $to_complete as $complete_run ) {
$pass_args = $complete_run;
$return_args = $this->run_trigger( $pass_args );
$complete_run['return_args'] = $return_args;
do_action( 'uap_wc_save_order_item_meta_by_term', $complete_run );
$this->complete_trigger( $return_args );
}
}