Filter
uncanny-automator-pro
automator_woocommerce_subscription_token_parser
Filters the subscription token with parsing pieces and trigger data before replacement.
add_filter( 'automator_woocommerce_subscription_token_parser', $callback, 10, 4 );
Description
Filters the value for WooCommerce subscription tokens, like renewal count. This hook allows developers to modify or replace the parsed token value before it's used in an automation. It's applied after initial parsing and before the final output, offering flexibility for custom subscription data manipulation.
Usage
add_filter( 'automator_woocommerce_subscription_token_parser', 'your_function_name', 10, 4 );
Parameters
-
$value(mixed) - This parameter represents the value of the token that is being processed and potentially modified.
-
$pieces(mixed) - This parameter represents the value that is being processed and potentially modified by the filter.
-
$trigger_data(mixed) - This parameter is used to determine which specific WooCommerce subscription-related tokens are being processed.
-
$replace_args(mixed) - This parameter contains data related to the trigger that initiated the automation, providing context for token replacement.
Return Value
The filtered value.
Examples
add_filter( 'automator_woocommerce_subscription_token_parser', 'my_custom_wc_subscription_renewal_count_parser', 10, 4 );
/**
* Custom logic to parse and format the WooCommerce subscription renewal count token.
*
* This function intercepts the output of the 'automator_woocommerce_subscription_token_parser' hook
* specifically when the renewal count token is being processed. It allows for custom formatting
* or conditional logic based on the renewal count.
*
* @param mixed $value The original value of the token.
* @param array $pieces An array of token pieces.
* @param array $trigger_data The data associated with the trigger.
* @param array $replace_args The arguments used for replacement.
*
* @return mixed The modified or original token value.
*/
function my_custom_wc_subscription_renewal_count_parser( $value, $pieces, $trigger_data, $replace_args ) {
// Check if we are dealing with the renewal count token.
// Assuming 'RENEWAL_COUNT' is a common piece for this token.
if ( in_array( 'RENEWAL_COUNT', $pieces, true ) ) {
// Ensure $value is a numeric type before manipulation.
if ( is_numeric( $value ) ) {
$renewal_count = (int) $value;
// Example: Append a descriptive string based on the renewal count.
if ( $renewal_count === 1 ) {
return sprintf( __( 'First renewal (%s)', 'your-text-domain' ), $renewal_count );
} elseif ( $renewal_count > 1 && $renewal_count < 5 ) {
return sprintf( __( 'Renewal cycle %s', 'your-text-domain' ), $renewal_count );
} else {
return sprintf( __( 'Subscription renewal: %s', 'your-text-domain' ), $renewal_count );
}
}
}
// Return the original value if it's not the renewal count token or if processing failed.
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:1027
public function parse_subscription_tokens_pro( $value, $pieces, $recipe_id, $trigger_data, $user_id, $replace_args ) {
$to_match = array(
'WCSUBSCRIPTIONSTATUSCHANGED',
'WCSUBSCRIPTIONSUBSCRIBE',
'WCSUBSCRIPTIONVARIATION',
'WCSPECIFICSUBVARIATION',
'WCVARIATIONSUBSCRIPTIONEXPIRED',
'WCVARIATIONSUBSCRIPTIONSTATUSCHANGED',
'WOOSUBSCRIPTIONSTATUS',
'WOOSUBSCRIPTIONSTATUS_ID',
'WOOSUBSCRIPTIONSTATUS_END_DATE',
'WOOSUBSCRIPTIONSTATUS_TRIAL_END_DATE',
'WOOSUBSCRIPTIONS_SUBSCRIPTION_ID',
'WOOSUBSCRIPTIONS_SUBSCRIPTION_STATUS',
'WOOSUBSCRIPTIONS_SUBSCRIPTION_END_DATE',
'WOOSUBSCRIPTIONS_SUBSCRIPTION_TRIAL_END_DATE',
'WOOSUBSCRIPTIONS_SUBSCRIPTION_NEXT_PAYMENT_DATE',
'WOOSUBSCRIPTIONS_SUBSCRIPTION_RENEWAL_COUNT',
'WOOVARIPRODUCT_SUBSCRIPTION_RENEWAL_COUNT',
'WOOSUBSCRIPTIONS_THUMB_URL',
'WOOSUBSCRIPTIONS_THUMB_ID',
'WOOSUBSCRIPTIONS_ID',
'WOOSUBSCRIPTIONS_URL',
'WOOSUBSCRIPTIONS',
'WCSUBSCRIPTIONTRIALEXPIRES',
'WCVARIATIONSUBSCRIPTIONRENEWED',
'WCVARIATIONSUBSCRIPTIONTRIALEXPIRES',
'WCSUBSCRIPTIONSSWITCHED',
'WC_SUBSCRIPTION_RENEWAL_COUNT',
'RENEWAL_COUNT',
);
if ( $pieces ) {
if ( array_intersect( $to_match, $pieces ) ) {
$value = $this->replace_wcs_values_pro( $value, $pieces, $recipe_id, $trigger_data, $user_id, $replace_args );
$value = apply_filters( 'automator_woocommerce_subscription_token_parser', $value, $pieces, $trigger_data, $replace_args );
}
}
return $value;
}