automator_woocommerce_token_parser
Filters the parsed value of a WooCommerce token before it's used in an automation.
add_filter( 'automator_woocommerce_token_parser', $callback, 10, 2 );
Description
Filters the parsed value of WooCommerce tokens before they are outputted. Developers can modify the token's value, especially useful for formatting or transforming specific order details like product names or quantities. This hook fires after a token has been identified and its initial value retrieved but before it's inserted into the automation.
Usage
add_filter( 'automator_woocommerce_token_parser', 'your_function_name', 10, 2 );
Parameters
-
$value(mixed) - - **$token** `mixed`
-
$token_pieces(mixed) - - **$order** `mixed`
Return Value
The filtered value.
Examples
<?php
/**
* Modify the token value based on specific conditions.
*
* This example demonstrates how to intercept and alter the value of a WooCommerce token
* generated by Uncanny Automator. It checks if the token represents the order total
* and if the order is pending payment. If both conditions are true, it applies a discount
* to the order total before returning it.
*
* @param mixed $value The original token value.
* @param mixed $token The token string being parsed.
* @param array $token_pieces An array of strings representing the parsed token.
* @param WC_Order $order The current WooCommerce order object.
*
* @return mixed The modified token value.
*/
function my_automator_modify_wc_token_value( $value, $token, $token_pieces, $order ) {
// Check if the token is for the order total and the order status is 'pending-payment'.
// 'wc_order_total' is a common token for the order total in Uncanny Automator.
// Adjust 'wc_order_total' if your specific token for order total differs.
if ( $token === 'wc_order_total' && $order && $order->get_status() === 'pending-payment' ) {
// Ensure the value is a valid number before attempting calculations.
if ( is_numeric( $value ) ) {
// Apply a 5% discount to the order total for pending payment orders.
$discount_percentage = 0.05;
$discount_amount = $value * $discount_percentage;
$modified_value = $value - $discount_amount;
// Return the modified value.
return wc_price( $modified_value ); // Format as currency
}
}
// If the conditions are not met, return the original value.
return $value;
}
add_filter( 'automator_woocommerce_token_parser', 'my_automator_modify_wc_token_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:781
uncanny-automator-pro/src/integrations/woocommerce/tokens/wc-pro-tokens.php:1507
uncanny-automator-pro/src/integrations/woocommerce/tokens/wc-pro-tokens.php:2564
uncanny-automator-pro/src/integrations/woocommerce/tokens/wc-pro-tokens.php:2685
break;
}
$token = $parse;
$token_pieces = $pieces;
/**
* @since 3.2
*/
$value = apply_filters( 'automator_woocommerce_token_parser', $value, $token, $token_pieces, $order );
}
}
return $value;
}
/**