Filter uncanny-automator-pro

automator_woocommerce_order_item_created_token_parser

Filters the value of an order item token after it has been created during WooCommerce order processing.

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

Description

This filter allows modification of the token value after it's parsed for WooCommerce order items. Developers can alter the `$value` before it's used, useful for custom formatting or conditional logic based on the parsed token, the `$order_item`, or the `$order`.


Usage

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

Parameters

$value (mixed)
This parameter holds the current value being processed and potentially modified by the filter.
$parse (mixed)
This parameter holds the processed value of the token being parsed, which can be modified by the filter.
$pieces (mixed)
This parameter is used to specify how the token data should be parsed or formatted.
$order_item (mixed)
This parameter is a mixed array containing the parsed order item data, broken down into smaller pieces for easier manipulation.
$order (mixed)
This parameter represents the WooCommerce order object associated with the order item.

Return Value

The filtered value.


Examples

add_filter( 'automator_woocommerce_order_item_created_token_parser', 'my_automator_woocommerce_order_item_created_token_parser_logic', 10, 5 );

/**
 * Example filter to modify the parsed value for WooCommerce order item tokens in Uncanny Automator.
 *
 * This example demonstrates how to access and potentially alter the data being passed through
 * the automator_woocommerce_order_item_created_token_parser filter.
 *
 * @param mixed  $value       The current value being parsed for the token.
 * @param mixed  $parse       The parsed token data.
 * @param mixed  $pieces      An array of token pieces.
 * @param WC_Order_Item $order_item The current WooCommerce order item object.
 * @param WC_Order $order       The parent WooCommerce order object.
 *
 * @return mixed The modified or original value.
 */
function my_automator_woocommerce_order_item_created_token_parser_logic( $value, $parse, $pieces, $order_item, $order ) {
    // Example: If the token is for the product name and the order item is a simple product,
    // prepend a custom string.
    if ( isset( $pieces[0] ) && $pieces[0] === 'product' && isset( $pieces[1] ) && $pieces[1] === 'name' ) {
        // Check if the order item is a WC_Product_Variation or has a parent
        if ( $order_item instanceof WC_Product_Variation ) {
            // It's a variation, perhaps we want to show the parent product name as well
            $parent_product = wc_get_product( $order_item->get_parent_id() );
            if ( $parent_product ) {
                $value = $parent_product->get_name() . ' - ' . $value;
            }
        } elseif ( $order_item instanceof WC_Product ) {
            // It's a simple product
            $value = 'Custom Product Prefix: ' . $value;
        }
    }

    // Another example: If the token represents a custom meta field, and we want to format it.
    // This assumes the $parse variable might contain information about the meta key.
    // This is highly dependent on how Uncanny Automator structures $parse for meta fields.
    // For demonstration, let's assume $parse might be an array like ['meta', 'your_meta_key'].
    if ( is_array( $pieces ) && count( $pieces ) > 1 && $pieces[0] === 'meta' ) {
        $meta_key = $pieces[1];
        // Let's say we want to format a 'shipping_notes' meta field.
        if ( $meta_key === 'shipping_notes' && ! empty( $value ) ) {
            $value = 'Shipping Notes: ' . esc_html( $value );
        }
    }

    // Always return the value, whether modified or not.
    return $value;
}

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/tokens/wc-pro-tokens.php:2561

if ( ! empty( $tag_names ) ) {
							$value = join( ',', $tag_names );
						}
					}
					break;
				default:
					$value = $this->handle_default_switch( $value, $parse, $pieces, $order );
					$value = apply_filters( 'automator_woocommerce_order_item_created_token_parser', $value, $parse, $pieces, $order_item, $order );
					break;
			}
			$value = apply_filters( 'automator_woocommerce_token_parser', $value, $parse, $pieces, $order );
		}

		return $value;
	}


Scroll to Top