Filter uncanny-automator

automator_surecart_hydrate_common_tokens

Filters SureCart tokens when hydrating common tokens for automation.

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

Description

This filter hook allows developers to modify the output for common SureCart tokens before they are inserted into automations. It fires after a SureCart token's value has been determined but before it's applied. Developers can use this to alter token values or return entirely new ones based on the token, arguments, and trigger context.


Usage

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

Parameters

$output (mixed)
This parameter holds the output generated by the filter, which will be used to replace the placeholder token.
$token (mixed)
This parameter is used to return the processed token value.
$args (mixed)
This parameter contains the identifier for the specific token being processed.
$trigger (mixed)
This parameter contains the arguments passed to the automator action.

Return Value

The filtered value.


Examples

/**
 * Example of how to filter the automator_surecart_hydrate_common_tokens hook.
 *
 * This example demonstrates how to add custom logic to modify or add to
 * the common tokens available for SureCart integrations within the Automator plugin.
 * For instance, we could conditionally format the store URL or add a new token
 * based on the trigger.
 *
 * @param mixed $output  The current output for the token.
 * @param mixed $token   The token key (e.g., 'STORE_NAME', 'STORE_URL').
 * @param mixed $args    Additional arguments passed to the hook.
 * @param mixed $trigger The trigger object.
 *
 * @return mixed The modified or original output.
 */
add_filter( 'automator_surecart_hydrate_common_tokens', function( $output, $token, $args, $trigger ) {

	// Let's assume $args might contain an 'account' object if it's available.
	// In a real scenario, you'd inspect $args and $trigger more thoroughly
	// to understand what data is available.
	$account = isset( $args['account'] ) ? $args['account'] : null;

	// Example: Conditionally modify the STORE_URL for a specific trigger type.
	if ( $token === 'STORE_URL' && $account && isset( $trigger['name'] ) && 'customer_registered' === $trigger['name'] ) {
		// Append a UTM parameter for tracking specific customer registration sources.
		$output = $account->url . '?utm_source=automator-customer-reg';
	}

	// Example: Add a new, custom token if certain conditions are met.
	if ( $token === 'CUSTOM_STORE_INFO' && $account ) {
		// This token is hypothetical and would need to be defined elsewhere
		// for the Automator plugin to recognize it initially.
		// Here we are showing how to *provide* its value.
		$output = sprintf( 'Store: %s (%s)', $account->name, $account->url );
	}

	// Always return the potentially modified output.
	return $output;

}, 10, 4 );

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/surecart/tokens/surecart-tokens.php:379

public function hydrate_common_tokens( $token, $args, $trigger ) {

		$account = SureCartModelsAccount::find();

		$output = '';

		switch ( $token['id'] ) {
			case 'STORE_NAME':
				$output = $account->name;
				break;
			case 'STORE_URL':
				$output = $account->url;
				break;
		}

		return apply_filters( 'automator_surecart_hydrate_common_tokens', $output, $token, $args, $trigger );
	}


Scroll to Top