Filter uncanny-automator

automator_wholesale_suite_parse_order_tokens

Filters the order tokens processed by Wholesale Suite when an order is received.

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

Description

This filter allows developers to modify the available Wholesale Suite order tokens before they are parsed. It's useful for adding custom order-related tokens or altering existing ones. The `$pieces` parameter contains details about the token being processed.


Usage

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

Parameters

$pieces (mixed)
This parameter represents an array containing a specific token, likely indicating that the order has been received within the Wholesale Suite.

Return Value

The filtered value.


Examples

/**
 * Example function to modify the order tokens for Wholesale Suite triggers.
 *
 * This function demonstrates how to add custom data or modify existing order tokens
 * when a Wholesale Suite trigger fires, specifically for the 'WSS_ORDER_RECEIVED' event.
 *
 * @param array $tokens The array of order tokens to be parsed. Defaults to ['WSS_ORDER_RECEIVED'].
 * @param array $args   An array containing trigger context:
 *                      'pieces'       => The pieces of the trigger.
 *                      'recipe_id'    => The ID of the automation recipe.
 *                      'trigger_data' => Data related to the trigger.
 *                      'user_id'      => The ID of the user associated with the trigger.
 * @return array The modified array of order tokens.
 */
add_filter( 'automator_wholesale_suite_parse_order_tokens', function( $tokens, $args ) {

	// Ensure we have the necessary arguments.
	if ( ! isset( $args['pieces'] ) || ! isset( $args['trigger_data']['order_id'] ) ) {
		return $tokens;
	}

	$order_id = $args['trigger_data']['order_id'];
	$order = wc_get_order( $order_id );

	// Check if the order exists and is valid.
	if ( ! $order ) {
		return $tokens;
	}

	// Add a custom token representing the customer's first name if it exists.
	if ( $customer_first_name = $order->get_billing_first_name() ) {
		$tokens['WSS_ORDER_CUSTOMER_FIRST_NAME'] = $customer_first_name;
	}

	// Add a token for the total order amount.
	$tokens['WSS_ORDER_TOTAL_AMOUNT'] = $order->get_total();

	// You could also dynamically add tokens based on the 'pieces' or other data.
	// For example, if the second piece indicates a specific product was ordered:
	// if ( isset( $args['pieces'][1] ) && 'specific_product_trigger_slug' === $args['pieces'][1] ) {
	//     $tokens['WSS_SPECIFIC_PRODUCT_INFO'] = 'Some custom info about the specific product.';
	// }

	return $tokens;

}, 10, 2 );

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/wholesale-suite/tokens/wss-tokens.php:213

*/
	public function parse_wss_tokens( $value, $pieces, $recipe_id, $trigger_data, $user_id, $replace_args ) {

		if ( ! is_array( $pieces ) || ! isset( $pieces[1] ) || ! isset( $pieces[2] ) ) {
			return $value;
		}

		$trigger_order_tokens = apply_filters(
			'automator_wholesale_suite_parse_order_tokens',
			array( 'WSS_ORDER_RECEIVED' ),
			array(
				'pieces'       => $pieces,
				'recipe_id'    => $recipe_id,
				'trigger_data' => $trigger_data,
				'user_id'      => $user_id,


Scroll to Top