Filter uncanny-automator

automator_woocommerce_product_meta_token_parser

Filters product meta token parsing to allow modification of values before they are inserted into content.

add_filter( 'automator_woocommerce_product_meta_token_parser', $callback, 10, 4 );

Description

Fires when parsing WooCommerce product meta data for tokens. Developers can use this filter to modify the parsed value before it's returned. This hook is useful for transforming or adding custom logic to how product meta fields are represented as tokens in automations.


Usage

add_filter( 'automator_woocommerce_product_meta_token_parser', 'your_function_name', 10, 4 );

Parameters

$value (mixed)
This parameter contains the original value that is being processed and potentially modified by the filter.
$meta_key (mixed)
This parameter holds the value that will be potentially modified and returned by the filter.
$pieces (mixed)
This parameter contains the meta key associated with the product data being processed.
$product (mixed)
This parameter contains an array of strings that represent the different parts or segments of the meta key being processed.

Return Value

The filtered value.


Examples

/**
 * Example of how to use the 'automator_woocommerce_product_meta_token_parser' filter.
 * This example demonstrates how to modify the output for a specific meta key,
 * for instance, adding a prefix to the product's SKU.
 *
 * @param mixed $value     The original value of the product meta.
 * @param mixed $meta_key  The meta key of the product meta.
 * @param mixed $pieces    An array of pieces, often used for token structure.
 * @param WC_Product $product The WooCommerce product object.
 * @return mixed The modified or original value.
 */
add_filter( 'automator_woocommerce_product_meta_token_parser', function( $value, $meta_key, $pieces, $product ) {

	// Check if we're targeting a specific meta key, e.g., '_sku'
	if ( '_sku' === $meta_key ) {
		// Ensure the value is not empty before adding a prefix
		if ( ! empty( $value ) ) {
			// Prepend a custom prefix to the SKU
			$value = 'PROD-' . $value;
		}
	}

	// You could also add logic for other meta keys or conditions.
	// For example, to modify a custom meta field:
	// if ( 'my_custom_product_field' === $meta_key ) {
	//     $value = 'Processed: ' . $value;
	// }

	// Return the (potentially modified) value
	return $value;

}, 10, 4 ); // Priority 10, 4 accepted arguments

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

public function replace_product_only_values( $value, $pieces, $recipe_id, $trigger_data, $user_id, $replace_args ) {
		if ( empty( $trigger_data ) || empty( $replace_args ) ) {
			return $value;
		}
		$meta_key   = $pieces[2];
		$product_id = Automator()->db->token->get( 'product_id', $replace_args );
		if ( empty( $product_id ) ) {
			return $value;
		}
		$product = wc_get_product( $product_id );
		if ( ! $product instanceof WC_Product ) {
			return $value;
		}
		switch ( $meta_key ) {
			case 'WOOPRODUCT':
				$value = get_the_title( $product_id );
				break;
			case 'WOOPRODUCT_ID':
				$value = $product_id;
				break;
			case 'WOOPRODUCT_PRODUCT_PRICE':
				$value = wc_price( $product->get_price() );
				break;
			case 'WOOPRODUCT_PRODUCT_PRICE_UNFORMATTED':
				$value = $product->get_price();
				break;
			case 'WOOPRODUCT_PRODUCT_SALE_PRICE':
				$value = wc_price( $product->get_sale_price() );
				break;
			case 'WOOPRODUCT_PRODUCT_SALE_PRICE_UNFORMATTED':
				$value = $product->get_sale_price();
				break;
			case 'WOOPRODUCT_SKU':
				$value = $product->get_sku();
				break;
			case 'WOOPRODUCT_URL':
				$value = get_permalink( $product_id );
				break;
			case 'WOOPRODUCT_THUMB_ID':
				$value = get_post_thumbnail_id( $product_id );
				break;
			case 'WOOPRODUCT_THUMB_URL':
				$value = get_the_post_thumbnail_url( $product_id, 'full' );
				break;
			case 'NUMTIMES':
				$value = absint( $replace_args['run_number'] );
				break;
			case 'WOOPRODUCT_CATEGORIES':
				$return = array();
				$terms  = wp_get_post_terms( $product_id, 'product_cat' );
				if ( $terms ) {
					foreach ( $terms as $term ) {
						$return[] = $term->name;
					}
				}
				$value = join( ', ', $return );
				break;
			case 'WOOPRODUCT_TAGS':
				$return = array();
				$terms  = wp_get_post_terms( $product_id, 'product_tag' );
				if ( $terms ) {
					foreach ( $terms as $term ) {
						$return[] = $term->name;
					}
				}
				$value = join( ', ', $return );
				break;
			default:
				$value = apply_filters( 'automator_woocommerce_product_meta_token_parser', $value, $meta_key, $pieces, $product );
				break;
		}

		return $value;
	}

Scroll to Top