Filter uncanny-automator

automator_woocommerce_custom_item_meta_token_parser

Filters the value of a custom WooCommerce item meta token before it's parsed for automation.

add_filter( 'automator_woocommerce_custom_item_meta_token_parser', $callback, 10, 5 );

Description

Filters the value of custom WooCommerce order item meta when parsing tokens. Developers can use this hook to modify or customize how specific order item meta values are represented or processed within Uncanny Automator, offering granular control over token output.


Usage

add_filter( 'automator_woocommerce_custom_item_meta_token_parser', 'your_function_name', 10, 5 );

Parameters

$value (mixed)
This parameter holds the current meta value for the given meta key.
$meta_key (mixed)
This parameter represents the current value of the custom item meta being parsed.
$pieces (mixed)
This parameter represents the specific meta key associated with a WooCommerce order item.
$order (mixed)
This parameter contains an array of strings representing the individual components or segments of a custom meta key that has been split for parsing.
$item (mixed)
The `$order` parameter represents the WooCommerce order object associated with the item.

Return Value

The filtered value.


Examples

add_filter(
	'automator_woocommerce_custom_item_meta_token_parser',
	/**
	 * Example filter to modify a WooCommerce order item meta value for Uncanny Automator tokens.
	 *
	 * This example demonstrates how to check for a specific meta key and apply a transformation
	 * to its value before it's used as an Automator token. For instance, it might prepend a
	 * string to a meta value like 'SKU' or format a date.
	 *
	 * @param mixed        $value    The original meta value.
	 * @param mixed        $meta_key The meta key being processed.
	 * @param array        $pieces   An array representing the parsed token pieces (e.g., ['order', 'item_meta', 'my_meta_key']).
	 * @param WC_Order     $order    The WC_Order object.
	 * @param WC_Order_Item $item     The WC_Order_Item object (often WC_Order_Item_Product).
	 *
	 * @return mixed The potentially modified meta value.
	 */
	function( $value, $meta_key, $pieces, $order, $item ) {
		// Only apply this logic if we are processing a specific meta key, for example, '_custom_gift_message'.
		if ( '_custom_gift_message' === $meta_key ) {
			// Check if the value is not empty and is a string.
			if ( ! empty( $value ) && is_string( $value ) ) {
				// Prepend a prefix to the gift message.
				$value = 'Gift Message: ' . sanitize_text_field( $value );
			}
		}

		// Another example: If the meta key is '_product_delivery_date', format it.
		if ( '_product_delivery_date' === $meta_key ) {
			if ( ! empty( $value ) && ( $date = strtotime( $value ) ) ) {
				// Format the date to a more readable format.
				$value = date_i18n( get_option( 'date_format' ), $date );
			}
		}

		// Always return the value, modified or not.
		return $value;
	},
	10, // Priority
	5   // Number of accepted 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/tokens/wc-tokens.php:769
uncanny-automator-pro/src/integrations/woocommerce/tokens/wc-pro-tokens.php:1568

$items    = $order->get_items();
								if ( $items ) {
									/** @var WC_Order_Item_Product $item */
									foreach ( $items as $item ) {
										if ( $item->meta_exists( $meta_key ) ) {
											$value = $item->get_meta( $meta_key );
										}
										$value = apply_filters( 'automator_woocommerce_custom_item_meta_token_parser', $value, $meta_key, $pieces, $order, $item );
									}
								}
							}
						}
						break;
				}
				$token        = $parse;


Scroll to Top