Filter uncanny-automator

automator_wholesale_suite_save_order_tokens

Filters the order tokens saved when a Wholesale Suite order is received, allowing modification of these tokens.

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

Description

Fires when Wholesale Suite order tokens are being saved. Developers can filter the array of tokens to include custom tokens before they are persisted. This hook provides access to the order and trigger arguments.


Usage

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

Parameters

$args (mixed)
This parameter is an array containing predefined token identifiers, such as 'WSS_ORDER_RECEIVED', used to represent specific order-related events within the Wholesale Suite integration.

Return Value

The filtered value.


Examples

/**
 * Example function to filter and modify order tokens for Wholesale Suite.
 *
 * This function demonstrates how to hook into 'automator_wholesale_suite_save_order_tokens'
 * to add or remove order tokens that might be processed.
 *
 * @param array $order_tokens An array of order token slugs.
 * @param array $args         The arguments passed to the filter, likely containing trigger and entry details.
 * @return array The modified array of order tokens.
 */
function my_custom_automator_order_tokens( $order_tokens, $args ) {

	// Let's say we want to add a custom order token specific to our plugin
	// or if a certain condition is met in the $args.
	if ( isset( $args['entry_args']['code'] ) && 'WSS_ORDER_RECEIVED' === $args['entry_args']['code'] ) {
		// We can ensure our custom token is present.
		// For example, if we're dealing with a specific type of wholesale order.
		$custom_wholesale_token = 'MY_CUSTOM_WHOLESALE_ORDER_DETAILS';
		if ( ! in_array( $custom_wholesale_token, $order_tokens ) ) {
			$order_tokens[] = $custom_wholesale_token;
		}

		// Alternatively, if we wanted to remove a default token under certain circumstances:
		// $default_token_to_remove = 'WSS_ORDER_RECEIVED';
		// $order_tokens = array_diff( $order_tokens, array( $default_token_to_remove ) );
	}

	// Always return the modified array of tokens.
	return $order_tokens;
}

// Add the filter to WordPress.
// The priority is set to 10 (default) and it accepts 2 arguments: $order_tokens and $args.
add_filter( 'automator_wholesale_suite_save_order_tokens', 'my_custom_automator_order_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:41

public function save_token_data( $args, $trigger ) {
		if ( ! isset( $args['trigger_args'] ) || ! isset( $args['entry_args']['code'] ) ) {
			return;
		}

		$trigger_meta_order_tokens = apply_filters(
			'automator_wholesale_suite_save_order_tokens',
			array( 'WSS_ORDER_RECEIVED' ),
			$args
		);

		$trigger_meta_lead_tokens = apply_filters(
			'automator_wholesale_suite_save_lead_tokens',
			array( 'WSS_LEAD_CREATED' ),
			$args
		);

		if ( in_array( $args['entry_args']['code'], $trigger_meta_order_tokens ) ) {
			$order_id          = $args['trigger_args'][0];
			$trigger_log_entry = $args['trigger_entry'];
			if ( ! empty( $order_id ) ) {
				Automator()->db->token->save( 'order_id', $order_id, $trigger_log_entry );
			}
		}

		if ( in_array( $args['entry_args']['code'], $trigger_meta_lead_tokens ) ) {
			$new_lead          = $args['trigger_args'][0];
			$trigger_log_entry = $args['trigger_entry'];
			if ( is_object( $new_lead ) ) {
				Automator()->db->token->save( 'lead_id', $new_lead->ID, $trigger_log_entry );
			}
		}
	}


Scroll to Top