Filter
uncanny-automator-pro
automator_woocommerce_subscription_variation_product_token_value
Filter the token value before returning Filters the token value for WooCommerce subscription variations before it's returned, allowing modification of the dynamic data.
add_filter( 'automator_woocommerce_subscription_variation_product_token_value', $callback, 10, 3 );
Description
Filters the value for variation product tokens within WooCommerce Subscriptions. Developers can modify the token's output based on the token ID and the subscription object. Use this hook to customize how variation product data is represented in automations.
Usage
add_filter( 'automator_woocommerce_subscription_variation_product_token_value', 'your_function_name', 10, 3 );
Parameters
-
$token_value(mixed) - The value of the token
-
$token_id(string) - The token identifier
-
$subscription(WC_Subscription) - The subscription object
Return Value
The filtered value.
Examples
add_filter( 'automator_woocommerce_subscription_variation_product_token_value', 'my_custom_automator_variation_product_token_value', 10, 3 );
/**
* Custom function to modify the WooCommerce subscription variation product token value.
*
* This example demonstrates how to append a custom string to the product name
* if the token ID is 'variation_product_name' for a specific subscription.
*
* @param mixed $token_value The original token value (e.g., product name).
* @param string $token_id The identifier for the token (e.g., 'variation_product_name').
* @param WC_Subscription $subscription The WooCommerce Subscription object.
* @return mixed The modified token value.
*/
function my_custom_automator_variation_product_token_value( $token_value, $token_id, $subscription ) {
// Check if the token ID is for the variation product name and if the subscription is valid
if ( 'variation_product_name' === $token_id && $subscription instanceof WC_Subscription ) {
// You can add more complex logic here, for example, checking for specific product IDs or subscription statuses.
// For this example, we'll just append a string if it's the variation product name.
$token_value .= ' (Customized)';
}
// Always return the token value, whether modified or not.
return $token_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-subscription/tokens/variation-product-tokens.php:166
public function parse_token( $token_id, $subscription ) {
// Default value in case of errors
$token_value = '';
// Ensure we have a valid subscription object
if ( ! is_a( $subscription, 'WC_Subscription' ) ) {
return $token_value;
}
switch ( $token_id ) {
case $this->unique_code . $this->prefix . '_PRODUCT':
$token_value = $this->get_product_title( $subscription );
break;
case $this->unique_code . $this->prefix . '_PRODUCT_ID':
$token_value = $this->get_product_id( $subscription );
break;
case $this->unique_code . $this->prefix . '_PRODUCT_URL':
$product_id = $this->get_product_id( $subscription );
$token_value = get_permalink( $product_id );
break;
case $this->unique_code . $this->prefix . '_PRODUCT_THUMB_URL':
$product_id = $this->get_product_id( $subscription );
$token_value = $this->format_with_na( get_the_post_thumbnail_url( $product_id, 'full' ) );
break;
case $this->unique_code . $this->prefix . '_PRODUCT_THUMB_ID':
$product_id = $this->get_product_id( $subscription );
$token_value = $this->format_with_na( get_post_thumbnail_id( $product_id ) );
break;
}
/**
* Filter the token value before returning
*
* @param mixed $token_value The value of the token
* @param string $token_id The token identifier
* @param WC_Subscription $subscription The subscription object
*/
return apply_filters( 'automator_woocommerce_subscription_variation_product_token_value', $token_value, $token_id, $subscription );
}