Filter uncanny-automator

automator_surecart_hydrate_order_tokens_v2

Filters the order tokens during SureCart checkout to hydrate them with relevant checkout data.

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

Description

Filters the order tokens before they are hydrated for SureCart. Developers can modify or add to the order-related tokens available for use in automations, providing fine-grained control over data passed to the workflow. This hook fires during SureCart checkout processing.


Usage

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

Parameters

$output (mixed)
This parameter is not explicitly used or returned by the function.
$checkout (mixed)
This parameter is the mixed return value from the hook, which is initially populated by the function's default return value before being modified by any registered filters.

Return Value

The filtered value.


Examples

<?php
/**
 * Example function to modify SureCart order tokens.
 * This function demonstrates how to access and potentially alter
 * the order tokens before they are used by Automator.
 *
 * @param array  $output  The array of order tokens with their values.
 * @param object $checkout The SureCart checkout object.
 * @return array The modified array of order tokens.
 */
add_filter( 'automator_surecart_hydrate_order_tokens_v2', function( $output, $checkout ) {

	// Let's add a custom token if a specific payment processor is used.
	if ( isset( $checkout->payment_method ) && $checkout->payment_method->processor_type === 'stripe' ) {
		$output['ORDER_IS_STRIPE'] = true;
	} else {
		$output['ORDER_IS_STRIPE'] = false;
	}

	// We can also modify an existing token, for example, add a prefix to the order number.
	if ( isset( $output['ORDER_NUMBER'] ) ) {
		$output['ORDER_NUMBER'] = 'SC-' . $output['ORDER_NUMBER'];
	}

	// Ensure we always return the $output array.
	return $output;

}, 10, 2 ); // Priority 10, accepts 2 arguments ($output, $checkout)
?>

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-new-framework.php:513

public function hydrate_order_tokens( $checkout ) {

		$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;

			// Get product image data
			$image_data        = $this->get_product_image_data( $product->id );
			$product_image_id  = $image_data['id'];
			$product_image_url = $image_data['url'];

			if ( ! empty( $product_image_id ) ) {
				$product_thumbs[] = $product_image_id;
			}

			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;
			}
		}

		$output = array(
			'ORDER_PRODUCT'                 => implode( ', ', $product_names ),
			'ORDER_PRODUCT_ID'              => implode( ', ', $product_ids ),
			'ORDER_PRODUCT_THUMB'           => ! empty( $product_thumbs_url ) ? implode( ', ', $product_thumbs_url ) : '',
			'ORDER_PRODUCT_THUMB_ID'        => ! empty( $product_thumbs ) ? implode( ', ', $product_thumbs ) : '',
			'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_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_v2', $output, $checkout );
	}


Scroll to Top