Action uncanny-automator-pro

uap_wc_order_item_meta

Fires after a WooCommerce order item is processed, allowing for customization of meta data.

add_action( 'uap_wc_order_item_meta', $callback, 10, 4 );

Description

Fires when a WooCommerce order item is processed for Uncanny Automator Pro triggers. Developers can use this action to modify order item metadata, access order details, or perform custom actions before or after an item is processed by Uncanny Automator, for example, to prevent specific recipes from running based on item data.


Usage

add_action( 'uap_wc_order_item_meta', 'your_function_name', 10, 4 );

Parameters

$item (mixed)
This parameter provides the WooCommerce order item object.
$order_id (mixed)
This parameter contains the WooCommerce order item object, representing a specific product or service within the order.
$recipe_id (mixed)
This parameter contains the ID of the WooCommerce order that has had its status changed or an item created.
$args (mixed)
This parameter contains the ID of the recipe being processed.

Examples

<?php
/**
 * Example of using the uap_wc_order_item_meta action hook.
 * This function demonstrates how to access and potentially modify data associated with a WooCommerce order item
 * when an Uncanny Automator Pro trigger related to WooCommerce order items is being processed.
 *
 * In this realistic example, we'll assume we want to log the product name and its quantity if it's a specific product.
 *
 * @param int $item_id The ID of the WooCommerce order item.
 * @param int $order_id The ID of the WooCommerce order.
 * @param int $recipe_id The ID of the Uncanny Automator Pro recipe.
 * @param array $args The arguments passed from the trigger, potentially containing user and trigger information.
 */
function my_custom_uap_wc_order_item_meta_handler( $item_id, $order_id, $recipe_id, $args ) {

	// Ensure we have valid order and item IDs.
	if ( ! $item_id || ! $order_id || ! $recipe_id ) {
		return;
	}

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

	// Check if the order exists.
	if ( ! $order ) {
		return;
	}

	// Get the specific order item.
	$item = $order->get_item( $item_id );

	// Check if the item exists and is a product.
	if ( ! $item || ! $item->is_type( 'line_item' ) ) {
		return;
	}

	// Get product details.
	$product = $item->get_product();
	$product_name = $product ? $product->get_name() : 'Unknown Product';
	$quantity = $item->get_quantity();

	// Example: Log specific product information if a certain recipe is involved.
	// Replace 'YOUR_SPECIFIC_RECIPE_ID' with an actual recipe ID you are targeting.
	if ( absint( $recipe_id ) === 12345 ) {
		error_log( sprintf(
			'Uncanny Automator Pro: Processing order item meta for Recipe ID %d, Order ID %d, Item ID %d. Product: "%s", Quantity: %d',
			$recipe_id,
			$order_id,
			$item_id,
			$product_name,
			$quantity
		) );
	}

	// Example: You could also store custom meta data to the order item here if needed.
	// For instance, if you wanted to mark this item as processed by a specific automator.
	// $item->update_meta_data( '_automator_processed_recipe', $recipe_id );
	// $item->save();
}
add_action( 'uap_wc_order_item_meta', 'my_custom_uap_wc_order_item_meta_handler', 10, 4 );
?>

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-order-status-changed.php:129
uncanny-automator-pro/src/integrations/woocommerce/triggers/anon-order-item-created.php:135

public function woo_order_status_changed( $order_id ) {

		if ( ! $order_id ) {
			return;
		}
		$order = wc_get_order( $order_id );

		if ( ! $order ) {
			return;
		}

		$to_status = $order->get_status();

		$recipes          = Automator()->get->recipes_from_trigger_code( $this->trigger_code );
		$required_product = Automator()->get->meta_from_recipes( $recipes, 'WOOPRODUCT' );
		$required_status  = Automator()->get->meta_from_recipes( $recipes, $this->trigger_meta );
		$user_id          = $order->get_customer_id();
		$items            = $order->get_items();
		/** @var WC_Order_Item_Product $item */
		foreach ( $items as $item ) {
			$product_id = (int) $item->get_product_id();
			//Add where option is set to Any product
			foreach ( $recipes as $recipe_id => $recipe ) {
				foreach ( $recipe['triggers'] as $trigger ) {
					$trigger_id = absint( $trigger['ID'] );
					$recipe_id  = absint( $recipe_id );

					if ( ! isset( $required_product[ $recipe_id ] ) || ! isset( $required_product[ $recipe_id ][ $trigger_id ] ) ) {
						continue;
					}

					if ( intval( '-1' ) !== intval( $required_product[ $recipe_id ][ $trigger_id ] ) && (int) $required_product[ $recipe_id ][ $trigger_id ] !== $product_id ) {
						continue;
					}

					if ( intval( '-1' ) !== intval( $required_status[ $recipe_id ][ $trigger_id ] ) && str_replace( 'wc-', '', $required_status[ $recipe_id ][ $trigger_id ] ) !== $to_status ) {
						continue;
					}

					$args = $this->run_trigger( $user_id, $recipe_id, $trigger_id );
					//Adding an action to save order id in trigger meta
					do_action( 'uap_wc_trigger_save_meta', $order_id, $recipe_id, $args, 'product' );
					do_action( 'uap_wc_order_item_meta', $item->get_id(), $order_id, $recipe_id, $args );

					$this->complete_trigger( $args );
				}
			}
		}
	}


Scroll to Top