Filter uncanny-automator-pro

automator_pro_woocommerce_item_created_in_term_token_parser

Filters the value for term tokens when a WooCommerce item is created, allowing modification before it's used.

add_filter( 'automator_pro_woocommerce_item_created_in_term_token_parser', $callback, 10, 6 );

Description

Fires after a WooCommerce product term token value is determined. Developers can modify the token's output before it's used in an automation, enabling custom handling of product categories or tags for trigger or action inputs. This filter provides access to the parsed token details and related order/product data.


Usage

add_filter( 'automator_pro_woocommerce_item_created_in_term_token_parser', 'your_function_name', 10, 6 );

Parameters

$value (mixed)
This parameter holds the modified value of the token, which can be altered by the filter.
$parse (mixed)
This parameter holds the processed token value that will be returned if it matches a known WooCommerce product term type.
$pieces (mixed)
This parameter contains information about the parsing process for tokens.
$order (mixed)
This parameter contains an array of strings representing the different parts or components of the token being parsed.
$product_details (mixed)
This parameter represents the WooCommerce order object associated with the current action.
$this (mixed)
The `$this` parameter refers to the current instance of the `WC_Pro_Tokens` class, providing access to its methods and properties for token parsing.

Return Value

The filtered value.


Examples

/**
 * Example of how to hook into the automator_pro_woocommerce_item_created_in_term_token_parser filter.
 * This example demonstrates how to modify the value of a token related to product terms (categories/tags)
 * when a WooCommerce product is created and passed through the Uncanny Automator token parser.
 *
 * In this specific example, we'll assume the '$value' passed is an array of term IDs.
 * We'll check if the token being parsed is 'WOOPRODUCT_CATEGORIES' and if so,
 * we'll add a custom term ID to the list if it's not already present.
 *
 * @param mixed $value            The current value of the token.
 * @param mixed $parse            The parsed token string.
 * @param mixed $pieces           An array of token parts.
 * @param WC_Order|null $order     The current WooCommerce order object (if available).
 * @param array $product_details  Details about the product.
 * @param object $this            The instance of the token parser class.
 *
 * @return mixed The modified or original token value.
 */
add_filter( 'automator_pro_woocommerce_item_created_in_term_token_parser', function( $value, $parse, $pieces, $order, $product_details, $token_parser_instance ) {

    // We're primarily interested in modifying product term tokens.
    // The '$pieces' array typically contains the token name as the first element.
    if ( ! empty( $pieces[0] ) && in_array( $pieces[0], array( 'WOOPRODUCT_CATEGORIES', 'WOOPRODUCT_TAGS' ) ) ) {

        // Let's assume $value is an array of term IDs.
        if ( is_array( $value ) ) {

            // Example: Add a specific custom category term ID to the list if it's a category.
            // Replace '123' with your actual custom category term ID.
            $custom_term_id_to_add = 123;

            if ( $pieces[0] === 'WOOPRODUCT_CATEGORIES' && ! in_array( $custom_term_id_to_add, $value ) ) {
                $value[] = $custom_term_id_to_add;
            }

            // Example: Remove a specific term ID if it exists.
            // Replace '456' with the term ID you want to remove.
            $term_id_to_remove = 456;
            if ( ( $key = array_search( $term_id_to_remove, $value ) ) !== false ) {
                unset( $value[ $key ] );
                // Re-index the array to ensure it remains a proper array.
                $value = array_values( $value );
            }
        }
    }

    // Always return the value, whether modified or not.
    return $value;
}, 10, 6 );

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:2682

case 'WOOPRODUCT_CATEGORIES':
					$value = $this->get_product_related_values( 'categories' );
					break;
				case 'WOOPRODUCT_TAGS':
					$value = $this->get_product_related_values( 'tags' );
					break;
				default:
					$value = apply_filters( 'automator_pro_woocommerce_item_created_in_term_token_parser', $value, $parse, $pieces, $order, $product_details, $this );
					break;
			}
			$value = apply_filters( 'automator_woocommerce_token_parser', $value, $parse, $pieces, $order );
		}

		return $value;
	}


Scroll to Top