Filter uncanny-automator

automator_surecart_hydrate_product_tokens_v2

Filters product tokens for SureCart purchases, allowing modification of available data for automations.

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

Description

Filters the product tokens generated for SureCart purchases before they are used in automations. Developers can add, remove, or modify tokens for specific product details and associated purchase data. This hook offers granular control over tokenization for enhanced integration capabilities.


Usage

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

Parameters

$tokens (mixed)
This parameter contains an array of product tokens that are being hydrated for the purchase.
$purchase (mixed)
This parameter contains an array of tokens that represent product data, which will be filtered and potentially modified by the hooked function.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to use the automator_surecart_hydrate_product_tokens_v2 filter.
 * This example adds a custom token to the product tokens for SureCart purchases.
 * It checks if the product is a "digital" product and adds a 'is_digital_product' token.
 *
 * @param array $tokens   The existing tokens array.
 * @param object $purchase The SureCart purchase object.
 * @return array The modified tokens array.
 */
add_filter(
	'automator_surecart_hydrate_product_tokens_v2',
	function ( $tokens, $purchase ) {
		// Ensure $purchase is an object and has the necessary properties.
		if ( ! is_object( $purchase ) || ! isset( $purchase->products ) || ! is_array( $purchase->products ) ) {
			return $tokens;
		}

		// Assume for this example we want to check the first product in the purchase.
		// In a real-world scenario, you might iterate through all products or have a specific product ID to check.
		if ( ! empty( $purchase->products ) ) {
			$first_product = $purchase->products[0];

			// Check if the product is digital. This is a hypothetical check;
			// the actual way to determine if a SureCart product is digital
			// might involve checking specific meta data or properties.
			// Replace 'is_digital' with the actual method or property if known.
			$is_digital = false;
			if ( isset( $first_product->meta ) && is_array( $first_product->meta ) && isset( $first_product->meta['digital'] ) && $first_product->meta['digital'] ) {
				$is_digital = true;
			}

			// Add a custom token if the product is digital.
			if ( $is_digital ) {
				$tokens['is_digital_product'] = __( 'Yes', 'your-text-domain' );
			} else {
				$tokens['is_digital_product'] = __( 'No', 'your-text-domain' );
			}
		}

		return $tokens;
	},
	10,
	2 // Specify that the callback accepts 2 arguments
);
?>

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

public function hydrate_product_tokens( $purchase ) {

		$purchase_data = $this->get_hydrated_purchase( $purchase->id );

		$price = $this->get_price( $purchase_data );

		$amount         = $this->get_amount( $price );
		$price_type     = $this->get_price_type( $price );
		$download_names = $this->get_download_names( $purchase_data );
		$download_urls  = $this->get_download_urls( $purchase_data );
		$download_links = $this->get_download_links( $purchase_data );

		$tokens = array();

		$chekout = $purchase_data->initial_order->checkout;

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

		$parsed = array(
			'PRODUCT'                => $purchase_data->product->name,
			'PRODUCT_NAME'           => $purchase_data->product->name,
			'PRODUCT_ID'             => $purchase_data->product->id,
			'PRODUCT_THUMB_ID'       => $product_image_id,
			'PRODUCT_THUMB'          => $product_image_url,
			'PRODUCT_PRICE'          => $amount,
			'PRODUCT_PRICE_ID'       => isset( $price->id ) ? $price->id : '',
			'PRODUCT_PAYMENT_TYPE'   => $price_type,
			'PRODUCT_TRIAL_DAYS'     => empty( $price->trial_duration_days ) ? '' : $price->trial_duration_days,
			'PRODUCT_DOWNLOAD_URL'   => $download_urls,
			'PRODUCT_DOWNLOAD_LINK'  => $download_links,
			'PRODUCT_DOWNLOAD_TITLE' => $download_names,
			'ORDER_ID'               => $purchase_data->initial_order->id,
			'SUBSCRIPTION_ID'        => isset( $purchase_data->subscription->id ) ? $purchase_data->subscription->id : '',
			'ORDER_NUMBER'           => $purchase_data->initial_order->number,
			'ORDER_DATE'             => gmdate( get_option( 'date_format', 'F j, Y' ), $purchase_data->initial_order->created_at ),
			'ORDER_STATUS'           => $purchase_data->initial_order->status,
			'ORDER_PAID_AMOUNT'      => $this->format_amount( $chekout->charge->amount ),
			'ORDER_SUBTOTAL'         => $this->format_amount( $chekout->subtotal_amount ),
			'ORDER_TOTAL'            => $this->format_amount( $chekout->total_amount ),
			'PAYMENT_METHOD'         => isset( $chekout->payment_method->processor_type ) ? $chekout->payment_method->processor_type : '',
			'ORDER_COUPON'           => empty( $chekout->discount->coupon->name ) ? '' : $chekout->discount->coupon->name,
			'ORDER_DISCOUNT'         => $this->format_amount( $chekout->discount_amount ),
		);

		$tokens = array_merge( $tokens, $parsed );

		$shipping_tokens = $this->hydrate_shipping_tokens( $parsed, $purchase_data );
		$tokens          = array_merge( $tokens, $shipping_tokens );
		$billing_tokens  = $this->hydrate_billing_tokens( $chekout );
		$tokens          = array_merge( $tokens, $billing_tokens );

		return apply_filters( 'automator_surecart_hydrate_product_tokens_v2', $tokens, $purchase );
	}

Scroll to Top