Filter uncanny-automator

automator_studiocart_order_token_parser

Filters the parsed token value before it's used in StudioCart order processing.

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

Description

Allows developers to modify the parsed value of StudioCart order tokens before they are used. This filter provides access to the original token, its component parts, and the associated order object. Use it to customize token output or handle specific order data within StudioCart integrations.


Usage

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

Parameters

$value (mixed)
This parameter holds the value that will be returned by the filter, and it can be modified by a callback function.
$token (mixed)
The `$value` parameter represents the current value of the token being processed, which can be modified by the filter.
$token_pieces (mixed)
This parameter represents the specific token being parsed from the StudioCart order data.
$order (mixed)
This parameter holds an array of strings representing the individual parts of the token after it has been split.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to use the 'automator_studiocart_order_token_parser' filter.
 * This example will modify the output for a specific token,
 * for instance, to add a prefix to the order coupon code if one exists.
 */
add_filter(
	'automator_studiocart_order_token_parser',
	function ( $value, $token, $token_pieces, $order ) {
		// Check if we are dealing with the coupon code token and if a coupon was used.
		if ( 'coupon_code' === $token && isset( $order['discount']['code'] ) && ! empty( $order['discount']['code'] ) ) {
			// Add a prefix to the coupon code.
			$value = 'DISC-' . $order['discount']['code'];
		}

		// You can add more conditions here for other tokens or logic.
		// For example, to modify the order total for a specific display.
		if ( 'total_price' === $token ) {
			// Assuming $value is the total price as a string, convert it to float for manipulation.
			$total_price_float = floatval( $value );
			// Add a small handling fee for demonstration.
			$total_price_float += 1.50;
			// Format it back to a string with two decimal places.
			$value = number_format( $total_price_float, 2 );
		}

		// Always return the (potentially modified) value.
		return $value;
	},
	10, // Priority
	4  // Number of accepted 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/studiocart/tokens/studiocart-tokens.php:211
uncanny-automator-pro/src/integrations/studiocart/tokens/studiocart-pro-tokens.php:378
uncanny-automator-pro/src/integrations/studiocart/tokens/studiocart-pro-tokens.php:514

case 'payment_option_label':
								$scorder = new ScrtOrder( $order_id );
								$value   = $scorder->item_name;
								break;
							default:
								$token        = $parse;
								$token_pieces = $pieces;
								$value        = apply_filters( 'automator_studiocart_order_token_parser', $value, $token, $token_pieces, $order );
						}
					}
				}
			}
		}

		return $value;


Scroll to Top