Filter
uncanny-automator-pro
automator_pro_woocommerce_subscriptions_token_parser
Filters the token parser before processing WooCommerce subscription data.
add_filter( 'automator_pro_woocommerce_subscriptions_token_parser', $callback, 10, 4 );
Description
Filters the parsed value for WooCommerce Subscription tokens before it's outputted. Developers can use this hook to modify or conditionally alter the token's resolved data based on the subscription, parsing context, and original value. This hook fires after the initial token parsing logic.
Usage
add_filter( 'automator_pro_woocommerce_subscriptions_token_parser', 'your_function_name', 10, 4 );
Parameters
-
$value(mixed) - The `$value` parameter represents the data that will be processed or returned after token parsing.
-
$parse(mixed) - This parameter holds the token's parsed value before it's returned to the automator.
-
$pieces(mixed) - This parameter holds the identifier for the specific token being parsed from the WooCommerce subscription.
-
$subscription(mixed) - This parameter provides an array containing the individual parts of the subscription data being parsed.
Return Value
The filtered value.
Examples
/**
* Modify the subscription renewal date for specific subscription statuses.
*
* This filter hook allows us to intercept and potentially modify the value
* returned by the automator_pro_woocommerce_subscriptions_token_parser.
* In this example, we're checking if the subscription status is 'on-hold'
* and if so, we're adding 7 days to the renewal date before it's returned.
*
* @param mixed $value The original value parsed by the token.
* @param mixed $parse The parsed token string (e.g., 'subscription_renewal_date').
* @param mixed $pieces An array of pieces from the parsed token.
* @param WC_Subscription $subscription The WC_Subscription object.
* @return mixed The modified or original value.
*/
add_filter( 'automator_pro_woocommerce_subscriptions_token_parser', function( $value, $parse, $pieces, $subscription ) {
// Ensure we have a valid WC_Subscription object.
if ( ! $subscription instanceof WC_Subscription ) {
return $value;
}
// Check if the token being parsed is for the subscription renewal date.
// And if the subscription status is 'on-hold'.
if ( 'subscription_renewal_date' === $parse && 'on-hold' === $subscription->get_status() ) {
// Attempt to parse the existing renewal date.
try {
$renewal_date = new DateTime( $value );
// Add 7 days to the renewal date.
$renewal_date->modify( '+7 days' );
// Format the date back to the original format if possible, or a standard format.
// Assuming $value might be in 'Y-m-d H:i:s' or similar.
// If the original format is known and needs to be preserved precisely, adjust this line.
$value = $renewal_date->format( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) );
} catch ( Exception $e ) {
// Log an error or handle the exception if date parsing fails.
// For this example, we'll just return the original value.
error_log( 'Error parsing or modifying subscription renewal date: ' . $e->getMessage() );
}
}
// Always return the value, whether modified or not.
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
uncanny-automator-pro/src/integrations/woocommerce/tokens/wc-pro-tokens.php:1807
break;
default:
$this->handle_default_switch( $value, $parse, $pieces, $subscription );
break;
}
$value = apply_filters( 'automator_pro_woocommerce_subscriptions_token_parser', $value, $parse, $pieces, $subscription );
}
return $value;
}
/**
* @param $value