Filter uncanny-automator

automator_surecart_hydrate_billing_tokens

Filters the parsed billing tokens after SureCart data has been hydrated.

add_filter( 'automator_surecart_hydrate_billing_tokens', $callback, 10, 2 );

Description

Fires after SureCart billing information is parsed and before it's added to automator tokens. Developers can use this filter to modify or add custom billing-related tokens from SureCart, providing greater flexibility for automation workflows. Ensure returned data is an associative array.


Usage

add_filter( 'automator_surecart_hydrate_billing_tokens', 'your_function_name', 10, 2 );

Parameters

$parsed (mixed)
This parameter contains the parsed token data that is being processed for hydration.
$data (mixed)
This parameter holds the parsed billing token data, which might be modified by the filter.

Return Value

The filtered value.


Examples

add_filter( 'automator_surecart_hydrate_billing_tokens', 'my_custom_surecart_billing_tokens', 10, 2 );

/**
 * Custom function to add or modify billing tokens for SureCart integrations.
 *
 * This example demonstrates how to add a new custom token or modify existing ones
 * based on the provided billing data. For instance, we might want to include
 * a formatted "Full Address" token if the data is available.
 *
 * @param array $parsed The array of parsed tokens, including default billing information.
 * @param object $data   The raw data object from SureCart, likely containing checkout details.
 * @return array The modified array of tokens.
 */
function my_custom_surecart_billing_tokens( $parsed, $data ) {

	// Check if the $data object has a 'billing' property and if it's an object.
	if ( isset( $data->billing ) && is_object( $data->billing ) ) {

		// Example: Add a custom 'BILLING_FULL_ADDRESS' token if address parts are available.
		if ( ! empty( $data->billing->address_1 ) && ! empty( $data->billing->city ) && ! empty( $data->billing->state ) && ! empty( $data->billing->postcode ) ) {
			$parsed['BILLING_FULL_ADDRESS'] = sprintf(
				'%1$s, %2$s, %3$s %4$s',
				$data->billing->address_1,
				$data->billing->city,
				$data->billing->state,
				$data->billing->postcode
			);
		}

		// Example: Conditionally override or add a custom token based on a specific condition.
		// Let's say we want to append a country code if it's a specific country.
		if ( ! empty( $data->billing->country ) && 'US' === $data->billing->country ) {
			if ( isset( $parsed['BILLING_STATE'] ) ) {
				$parsed['BILLING_STATE'] .= ' (USA)';
			} else {
				$parsed['BILLING_STATE'] = ' (USA)'; // Should not happen if $data->billing->state is set.
			}
		}
	}

	// Always return the modified $parsed array.
	return $parsed;
}

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

public function hydrate_billing_tokens( $parsed, $data ) {

		$checkout = null;

		if ( is_a( $data, 'SureCartModelsPurchase' ) && isset( $data->initial_order->checkout ) ) {
			$checkout = $data->initial_order->checkout;
		}

		if ( is_a( $data, 'SureCartModelsCheckout' ) ) {
			$checkout = $data;
		}

		if ( null === $checkout ) {
			return $parsed;
		}

		$parsed = $parsed + array(
			'BILLING_NAME'       => empty( $checkout->name ) ? '' : $checkout->name,
			'BILLING_FIRST_NAME' => empty( $checkout->first_name ) ? '' : $checkout->first_name,
			'BILLING_LAST_NAME'  => empty( $checkout->last_name ) ? '' : $checkout->last_name,
			'BILLING_EMAIL'      => empty( $checkout->email ) ? '' : $checkout->email,
		);

		return apply_filters( 'automator_surecart_hydrate_billing_tokens', $parsed, $data );
	}

Scroll to Top