Action uncanny-automator

uap_wc_trigger_save_meta

Fires when saving WooCommerce order meta for User Access Policies, passing order and recipe IDs.

add_action( 'uap_wc_trigger_save_meta', $callback, 10, 3 );

Description

Fires after user meta is saved for WooCommerce triggers like product purchases or order status changes. Developers can use this hook to perform custom actions, such as updating additional user meta or triggering other automations based on the saved data. It provides access to the order ID, matched recipe ID, and arguments used during the trigger.


Usage

add_action( 'uap_wc_trigger_save_meta', 'your_function_name', 10, 3 );

Parameters

$order_id (mixed)
The ID of the WooCommerce order that triggered the hook.
$matched_recipe_id (mixed)
The ID of the WooCommerce order that triggered the action.
$args (mixed)
This parameter holds the ID of the recipe that was matched by the trigger.

Examples

<?php
/**
 * Example function to hook into 'uap_wc_trigger_save_meta' to save custom order meta.
 * This function demonstrates how to access and utilize the parameters provided by the hook
 * to store specific data related to the order and the matched recipe.
 *
 * @param int|WC_Order $order_id The ID or WC_Order object of the order.
 * @param int $matched_recipe_id The ID of the matched Uncanny Automator recipe.
 * @param array $args An array of arguments related to the trigger entry, potentially including trigger completion status.
 * @param string $product_identifier A string identifier, in this context 'product'.
 */
add_action( 'uap_wc_trigger_save_meta', function( $order_id, $matched_recipe_id, $args, $product_identifier ) {

	// Ensure we have a valid order object or ID to work with.
	if ( ! $order_id ) {
		return;
	}

	// If $order_id is an object, get its ID.
	if ( is_a( $order_id, 'WC_Order' ) ) {
		$order_id = $order_id->get_id();
	}

	// Only proceed if it's a valid order ID.
	if ( ! is_numeric( $order_id ) || $order_id <= 0 ) {
		return;
	}

	// Get the order object.
	$order = wc_get_order( $order_id );

	// Check if the order object was successfully retrieved.
	if ( ! $order ) {
		return;
	}

	// Example: Save the matched recipe ID and the trigger entry arguments as custom order meta.
	// This could be useful for debugging or for further automation based on this meta.
	$meta_key_recipe_id = '_uncanny_automator_matched_recipe_id';
	$meta_key_trigger_args = '_uncanny_automator_trigger_args';

	// Update or add the meta for the matched recipe ID.
	update_post_meta( $order_id, $meta_key_recipe_id, absint( $matched_recipe_id ) );

	// If the $args are not empty, save them as serialized data.
	if ( ! empty( $args ) && is_array( $args ) ) {
		update_post_meta( $order_id, $meta_key_trigger_args, $args );
	}

	// Example: If the product_identifier is 'product', we might do something specific.
	if ( 'product' === $product_identifier ) {
		// For instance, log that this specific trigger was saved for a product.
		error_log( sprintf(
			'Uncanny Automator: Saved meta for order ID %d, matched recipe ID %d, using product identifier.',
			$order_id,
			$matched_recipe_id
		) );
	}

}, 10, 4 ); // Priority 10, accepting 4 arguments.
?>

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

src/integrations/woocommerce/triggers/wc-purchasesproduct.php:214
src/integrations/woocommerce/triggers/wc-purchprod-dep.php:167
src/integrations/upsell-plugin/triggers/upsell-plugin-purchprod.php:149
uncanny-automator-pro/src/integrations/woocommerce/triggers/anon-wc-orderrefunded.php:94
uncanny-automator-pro/src/integrations/woocommerce/triggers/wc-orderstatuschange.php:127
uncanny-automator-pro/src/integrations/woocommerce/triggers/wc-purchprod-tag-dep.php:185
uncanny-automator-pro/src/integrations/woocommerce/triggers/wc-order-payment-fail.php:94
uncanny-automator-pro/src/integrations/woocommerce/triggers/wc-purchprod-payment-gateway.php:209
uncanny-automator-pro/src/integrations/woocommerce/triggers/wc-quantity-purchprod.php:213
uncanny-automator-pro/src/integrations/woocommerce/triggers/anon-wc-orderpartrefunded.php:95
uncanny-automator-pro/src/integrations/woocommerce/triggers/anon-order-status-changed.php:128
uncanny-automator-pro/src/integrations/woocommerce/triggers/wc-ordercomplete.php:202
uncanny-automator-pro/src/integrations/woocommerce/triggers/anon-wc-purchvarprod.php:213
uncanny-automator-pro/src/integrations/woocommerce/triggers/anon-wc-orderrefundedasscproduct.php:194
uncanny-automator-pro/src/integrations/woocommerce/triggers/anon-wc-orderwithcoupon.php:182
uncanny-automator-pro/src/integrations/woocommerce/triggers/wc-purchprod-category-dep.php:196
uncanny-automator-pro/src/integrations/woocommerce/triggers/wc-orderproductqty.php:249
uncanny-automator-pro/src/integrations/woocommerce/triggers/wc-purchprod-category.php:229
uncanny-automator-pro/src/integrations/woocommerce/triggers/anon-wc-purchprod-category.php:198
uncanny-automator-pro/src/integrations/woocommerce/triggers/wc-purchprod-tag.php:226
uncanny-automator-pro/src/integrations/woocommerce/triggers/anon-wc-purchprod-dep.php:157
uncanny-automator-pro/src/integrations/woocommerce/triggers/wc-ordercomplete-dep.php:155
uncanny-automator-pro/src/integrations/woocommerce/triggers/wc-purchvariableprod.php:223
uncanny-automator-pro/src/integrations/woocommerce/triggers/anon-wc-orderstatuschange.php:131
uncanny-automator-pro/src/integrations/upsell-plugin/triggers/upsell-plugin-purchsubscription.php:152
uncanny-automator-pro/src/integrations/woocommerce/triggers/anon-wc-purchprod.php:183
uncanny-automator-pro/src/integrations/woocommerce/triggers/anon-order-item-created.php:134

'trigger_to_match' => $matched_recipe_id['trigger_id'],
				'ignore_post_id'   => true,
			);

			$args = Automator()->process->user->maybe_add_trigger_entry( $pass_args, false );

			//Adding an action to save order id in trigger meta
			do_action( 'uap_wc_trigger_save_meta', $order_id, $matched_recipe_id['recipe_id'], $args, 'product' );

			if ( $args ) {
				foreach ( $args as $result ) {
					if ( true === $result['result'] ) {
						do_action( 'automator_loopable_token_hydrate', $result['args'], $original_args );
						Automator()->process->user->maybe_trigger_complete( $result['args'] );
					}


Scroll to Top