Filter uncanny-automator

automator_surecart_hydrate_order_tokens

Filters SureCart order data for token hydration before it's used by the Automator.

add_filter( 'automator_surecart_hydrate_order_tokens', $callback, 10, 3 );

Description

Fires when SureCart order tokens are being hydrated. Allows developers to modify or add to the order-related tokens available for automation. This hook provides access to parsed token data, arguments, and the trigger object, enabling custom token values for SureCart orders.


Usage

add_filter( 'automator_surecart_hydrate_order_tokens', 'your_function_name', 10, 3 );

Parameters

$parsed (mixed)
This parameter represents the already parsed data before it's further processed for SureCart order tokens.
$args (mixed)
This parameter contains the parsed token data that is being processed by the automator.
$trigger (mixed)
This parameter contains the arguments passed to the trigger, which are used to retrieve the necessary checkout data.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to use the automator_surecart_hydrate_order_tokens filter.
 *
 * This filter allows you to modify or add custom tokens related to SureCart orders
 * before they are hydrated with actual data.
 *
 * In this example, we'll add a custom token 'ORDER_CUSTOMER_EMAIL' if the customer email
 * is available in the provided arguments.
 */
add_filter( 'automator_surecart_hydrate_order_tokens', function ( $parsed, $args, $trigger ) {

	// Check if the customer email is available in the arguments.
	// The structure of $args will depend on how the trigger was initiated.
	// Assuming $args might contain a 'customer' key with an 'email' property.
	if ( isset( $args['customer']['email'] ) && ! empty( $args['customer']['email'] ) ) {
		$parsed['ORDER_CUSTOMER_EMAIL'] = $args['customer']['email'];
	}

	// You can also modify existing tokens if needed.
	// For example, to append a prefix to the order number:
	// if ( isset( $parsed['ORDER_NUMBER'] ) && ! empty( $parsed['ORDER_NUMBER'] ) ) {
	// 	$parsed['ORDER_NUMBER'] = 'SC-' . $parsed['ORDER_NUMBER'];
	// }

	return $parsed;

}, 10, 3 );

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

public function hydrate_order_tokens( $parsed, $args, $trigger ) {

		$checkout = array_shift( $args['trigger_args'] );

		$checkout = SureCartModelsCheckout::with( array( 'purchases', 'purchase.product', 'purchase.line_items', 'line_item.price', 'payment_method' ) )->find( $checkout->id );

		foreach ( $checkout->purchases->data as $purchase_data ) {

			$product = $purchase_data->product;

			$product_names[] = $product->name;
			$product_ids[]   = $product->id;

			if ( ! empty( $product->image ) ) {
				$product_thumbs[] = $product->image;
			}

			if ( ! empty( $product->image_url ) ) {
				$product_thumbs_url[] = $product->image_url;
			}

			if ( ! empty( $product->subscription ) ) {
				$product_subscription_id[] = $product->subscription;
			}

			$price                = $this->get_price( $purchase_data );
			$product_amount[]     = $this->get_amount( $price );
			$product_price_type[] = $this->get_price_type( $price );

			if ( ! empty( $price->trial_duration_days ) ) {
				$product_trial_days[] = $price->trial_duration_days;
			}
		}

		$parsed = $parsed + array(
			'ORDER_PRODUCT'                 => implode( ', ', $product_names ),
			'ORDER_PRODUCT_ID'              => implode( ', ', $product_ids ),
			'ORDER_PRODUCT_THUMB'           => ! empty( $product_thumbs ) ? implode( ', ', $product_thumbs ) : '',
			'ORDER_PRODUCT_THUMB_ID'        => ! empty( $product_tproduct_thumbs_urlhumbs ) ? implode( ', ', $product_thumbs_url ) : '',
			'ORDER_PRODUCT_PRICE'           => implode( ', ', $product_amount ),
			'ORDER_PRODUCT_PAYMENT_TYPE'    => implode( ', ', $product_price_type ),
			'ORDER_PRODUCT_TRIAL_DAYS'      => ! empty( $product_trial_days ) ? implode( ', ', $product_trial_days ) : '',
			'ORDER_PRODUCT_SUBSCRIPTION_ID' => ! empty( $product_subscription_id ) ? implode( ', ', $product_subscription_id ) : '',
			'ORDER_AMOUNT_DUE'              => $this->format_amount( $checkout->amount_due ),
			'ORDER_APPLIED_BALANCE'         => $this->format_amount( $checkout->applied_balance_amount ),
			'ORDER_CREDITED_BALANCE'        => $this->format_amount( $checkout->credited_balance_amount ),
			'ORDER_DISCOUNT_AMOUNT'         => $this->format_amount( $checkout->discount_amount ),
			'ORDER_SUBTOTAL_AMOUNT'         => $this->format_amount( $checkout->subtotal_amount ),
			'ORDER_UNIT_AMOUNT'             => '',
			'ORDER_PAYMENT_PROCESSOR'       => $checkout->payment_method->processor_type,
			'ORDER_ID'                      => $checkout->order,
			'ORDER_NUMBER'                  => $checkout->number,
			'ORDER_PDF'                     => $checkout->pdf_url,
		);

		return apply_filters( 'automator_surecart_hydrate_order_tokens', $parsed, $args, $trigger );
	}


Scroll to Top