Filter uncanny-automator-pro

automator_woocommerce_subscription_token_value

Filter the token value before returning Filters the value of a WooCommerce subscription token before it is used or returned.

add_filter( 'automator_woocommerce_subscription_token_value', $callback, 10, 3 );

Description

Filters the value of a WooCommerce subscription token before it's returned by Uncanny Automator. Developers can modify the token's data to customize what information is available for triggers and actions based on subscription attributes. This hook fires when a specific subscription token's value is accessed.


Usage

add_filter( 'automator_woocommerce_subscription_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

// Modify the subscription's renewal total to exclude tax for a specific token
add_filter( 'automator_woocommerce_subscription_token_value', 'my_custom_automator_subscription_token_value', 10, 3 );

/**
 * Custom filter to modify the 'renewal_total' token value for WooCommerce Subscriptions.
 * This example aims to exclude tax from the renewal total when the token ID is 'renewal_total'.
 *
 * @param mixed         $token_value  The original token value.
 * @param string        $token_id     The identifier of the token.
 * @param WC_Subscription $subscription The subscription object.
 *
 * @return mixed The modified token value.
 */
function my_custom_automator_subscription_token_value( $token_value, $token_id, $subscription ) {
	// Check if we are dealing with the 'renewal_total' token
	if ( 'renewal_total' === $token_id ) {
		// Ensure the token_value is a valid number before proceeding
		if ( is_numeric( $token_value ) ) {
			// Get the subscription's tax total
			$tax_total = $subscription->get_total_tax();

			// Subtract tax from the renewal total if it exists
			if ( ! empty( $tax_total ) && is_numeric( $tax_total ) ) {
				$token_value = $token_value - $tax_total;
			}
		}
	}

	// Always return the (potentially modified) token value
	return $token_value;
}
// End of custom filter example

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/common-tokens.php:232

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:
				$token_value = $this->get_subscription_product_title( $subscription );
				break;
			case $this->unique_code . '_SUBSCRIPTION_ID':
				$token_value = $subscription->get_id();
				break;

			case $this->unique_code . '_SUBSCRIPTION_STATUS':
				$token_value = $subscription->get_status();
				break;

			case $this->unique_code . '_SUBSCRIPTION_TRIAL_END_DATE':
				// Using get_date() as per docs instead of get_trial_end()
				$token_value = $this->format_with_na( $subscription->get_date( 'trial_end' ) );
				break;

			case $this->unique_code . '_SUBSCRIPTION_END_DATE':
				// Using get_date() as per docs instead of get_end_date()
				$token_value = $this->format_with_na( $subscription->get_date( 'end' ) );
				break;

			case $this->unique_code . '_SUBSCRIPTION_NEXT_PAYMENT_DATE':
				// Using get_date() as per docs instead of get_next_payment_date()
				$token_value = $this->format_with_na( $subscription->get_date( 'next_payment' ) );
				break;

			case $this->unique_code . '_SUBSCRIPTION_RENEWAL_COUNT':
				// Using get_payment_count() as per docs
				$token_value = $subscription->get_payment_count( 'completed', array( 'renewal' ) );
				break;

			// Product related tokens
			case $this->unique_code . '_ID':
				$token_value = $this->get_subscription_product_id( $subscription );
				break;

			case $this->unique_code . '_URL':
				$product_id  = $this->get_subscription_product_id( $subscription );
				$token_value = get_permalink( $product_id );
				break;

			case $this->unique_code . '_THUMB_URL':
				$product_id  = $this->get_subscription_product_id( $subscription );
				$token_value = $this->format_with_na( get_the_post_thumbnail_url( $product_id, 'full' ) );
				break;

			case $this->unique_code . '_THUMB_ID':
				$product_id  = $this->get_subscription_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_token_value', $token_value, $token_id, $subscription );
	}

Scroll to Top