Filter uncanny-automator

automator_woocommerce_possible_tokens

Filters possible tokens available for WooCommerce automations, allowing customization before they are displayed.

add_filter( 'automator_woocommerce_possible_tokens', $callback, 10, 1 );

Description

Filters the list of available WooCommerce tokens for Uncanny Automator. Developers can add, remove, or modify tokens to be used in automations triggered by WooCommerce actions or events. This hook fires when Uncanny Automator is preparing to display tokens to the user.


Usage

add_filter( 'automator_woocommerce_possible_tokens', 'your_function_name', 10, 1 );

Parameters

$this (mixed)
This parameter represents the current object of the class handling WooCommerce tokens, providing access to its methods and properties.

Return Value

The filtered value.


Examples

/**
 * Add custom WooCommerce order tokens to Uncanny Automator.
 *
 * This filter hook allows developers to add or modify the list of available
 * WooCommerce order tokens that can be used within Uncanny Automator recipes.
 *
 * @param array $possible_tokens The current array of possible WooCommerce order tokens.
 * @return array The modified array of possible WooCommerce order tokens.
 */
add_filter(
	'automator_woocommerce_possible_tokens',
	function ( $possible_tokens ) {
		// Example: Add a custom token for the 'order_notes' field.
		// This assumes that $possible_tokens is an array where keys are the token IDs
		// and values are the display titles for those tokens.

		// Check if the token already exists to avoid duplicates.
		if ( ! isset( $possible_tokens['order_notes'] ) ) {
			$possible_tokens['order_notes'] = __( 'Order Notes', 'your-text-domain' );
		}

		// Example: Add a token for a custom order meta field, e.g., 'custom_delivery_date'.
		// Ensure this meta key actually exists in your WooCommerce orders if you are using it.
		if ( ! isset( $possible_tokens['custom_delivery_date'] ) ) {
			$possible_tokens['custom_delivery_date'] = __( 'Custom Delivery Date', 'your-text-domain' );
		}

		// You could also potentially remove tokens if needed, though this is less common.
		// For instance, if you wanted to remove 'order_date':
		// if ( isset( $possible_tokens['order_date'] ) ) {
		//     unset( $possible_tokens['order_date'] );
		// }

		return $possible_tokens;
	},
	10, // Priority: Default is 10. Adjust if needed to run before or after other filters.
	1   // Accepted args: The filter expects only one argument ($possible_tokens).
);

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:234
uncanny-automator-pro/src/integrations/woocommerce/tokens/wc-pro-tokens.php:820

public function wc_possible_tokens( $tokens = array(), $args = array(), $type = 'order' ) {
		if ( ! automator_do_identify_tokens() ) {
			return $tokens;
		}

		$fields           = array();
		$trigger_meta     = $args['meta'];
		$add_order_tokens = true;
		if ( isset( $args['triggers_meta']['code'] ) && 'VIEWWOOPRODUCT' === $args['triggers_meta']['code'] ) {
			$add_order_tokens = false;
		}
		$possible_tokens = array();
		if ( $add_order_tokens ) {
			$possible_tokens = apply_filters( 'automator_woocommerce_possible_tokens', $this->possible_order_fields );
		}
		foreach ( $possible_tokens as $token_id => $input_title ) {
			if ( 'billing_email' === (string) $token_id || 'shipping_email' === (string) $token_id ) {
				$input_type = 'email';
			} elseif ( 'order_qty' === (string) $token_id ) {
				$input_type = 'int';
			} else {
				$input_type = 'text';
			}
			$fields[] = array(
				'tokenId'         => $token_id,
				'tokenName'       => $input_title,
				'tokenType'       => $input_type,
				'tokenIdentifier' => $trigger_meta,
			);
		}
		$tokens = array_merge( $tokens, $fields );

		return Automator()->utilities->remove_duplicate_token_ids( $tokens );
	}

Scroll to Top