Filter
uncanny-automator
automator_woocommerce_custom_order_meta_token_parser
Filters custom order meta tokens for use in automations, allowing modification of their values before parsing.
add_filter( 'automator_woocommerce_custom_order_meta_token_parser', $callback, 10, 1 );
Description
Filters the value of custom order meta tokens before they are parsed and inserted into automations. Developers can use this hook to modify, format, or conditionally alter custom order meta values fetched for use in Uncanny Automator's WooCommerce tokens, ensuring precise data integration.
Usage
add_filter( 'automator_woocommerce_custom_order_meta_token_parser', 'your_function_name', 10, 1 );
Return Value
The filtered value.
Examples
/**
* Processes custom order meta for Uncanny Automator tokens, allowing for custom formatting or filtering.
*
* This function intercepts the value of custom order meta before it's passed to Uncanny Automator's token system.
* It can be used to modify the data, extract specific parts, or apply conditional logic.
*
* @param mixed $value The original meta value retrieved from the order.
* @param string $meta_key The meta key of the custom order meta.
* @param array $pieces An array of string pieces representing the parsed token, e.g., ['custom_order_meta', 'my_meta_key'].
* @param WC_Order $order The WooCommerce order object the meta belongs to.
*
* @return mixed The processed meta value.
*/
add_filter( 'automator_woocommerce_custom_order_meta_token_parser', function ( $value, $meta_key, $pieces, $order ) {
// Example: If the meta key is 'billing_phone' and the value is empty, return a default placeholder.
if ( 'billing_phone' === $meta_key && empty( $value ) ) {
return __( 'N/A', 'your-text-domain' );
}
// Example: If the meta key is 'custom_delivery_notes' and the value is an array,
// join the elements with a comma and space for better readability in the token.
if ( 'custom_delivery_notes' === $meta_key && is_array( $value ) ) {
return implode( ', ', $value );
}
// Example: If the meta key is 'order_total' and the value is a string that looks like a number,
// format it as currency.
if ( 'order_total' === $meta_key && is_string( $value ) && ctype_digit( str_replace( array( '.', ',' ), '', $value ) ) ) {
return wc_price( $value );
}
// If no specific logic applies, return the original value.
return $value;
}, 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
src/integrations/woocommerce/tokens/wc-tokens.php:755
uncanny-automator-pro/src/integrations/woocommerce/tokens/wc-pro-tokens.php:1554
$meta_key = $custom_meta[1];
if ( $order->meta_exists( $meta_key ) ) {
$value = $order->get_meta( $meta_key );
if ( is_array( $value ) ) {
$value = join( $multi_line_separator, $value );
}
}
$value = apply_filters( 'automator_woocommerce_custom_order_meta_token_parser', $value, $meta_key, $pieces, $order );
}
}
if ( preg_match( '/custom_item_meta/', $parse ) ) {
$custom_meta = explode( '|', $parse );
if ( ! empty( $custom_meta ) && count( $custom_meta ) > 1 && 'custom_item_meta' === $custom_meta[0] ) {
$meta_key = $custom_meta[1];
$items = $order->get_items();