Filter
uncanny-automator
automator_surecart_hydrate_shipping_tokens
Filters SureCart shipping tokens to allow modification before they are hydrated into the automator.
add_filter( 'automator_surecart_hydrate_shipping_tokens', $callback, 10, 2 );
Description
Filters SureCart shipping address tokens before they are hydrated into your automation. Use this hook to modify or replace shipping city, line 1, line 2, and postcode values. This hook fires after SureCart shipping address data is retrieved but before it's made available as merge tags.
Usage
add_filter( 'automator_surecart_hydrate_shipping_tokens', 'your_function_name', 10, 2 );
Parameters
-
$parsed(mixed) - This parameter contains the parsed data, which can be modified before being returned.
-
$data(mixed) - This parameter contains the parsed data that will be further processed to hydrate shipping tokens.
Return Value
The filtered value.
Examples
add_filter( 'automator_surecart_hydrate_shipping_tokens', 'my_custom_automator_surecart_shipping_tokens', 10, 2 );
/**
* Custom function to modify SureCart shipping tokens for Automator.
*
* This function demonstrates how to add a custom shipping token or
* modify an existing one based on the provided data.
*
* @param array $parsed The array of shipping tokens to be parsed.
* @param array $data The original data passed to the filter.
* @return array The modified array of shipping tokens.
*/
function my_custom_automator_surecart_shipping_tokens( $parsed, $data ) {
// Example: Add a custom token for the shipping country code if it exists.
// Assuming $data might contain a structure where shipping country is accessible,
// for instance, in a nested 'shipping_address' object or directly.
// For realism, let's assume $data['shipping_address']['country_code'] might be available.
// First, let's ensure we have a shipping address object or equivalent data.
// The existing code suggests $shipping_address is available within the scope where the filter is applied.
// However, for this standalone example, we'll try to access it from $data if possible,
// or demonstrate how you might pass it if the hook allowed it.
// Since the hook is 'hydrate_shipping_tokens', it's reasonable to assume $parsed already contains basic shipping info.
// If $data contains a more complete address object that includes a country code, we can use that.
// For demonstration, let's simulate accessing a country code.
// In a real scenario, you would inspect $data's structure or assume it's passed in a predictable way.
$shipping_country_code = '';
if ( isset( $data['shipping_address']['country_code'] ) && ! empty( $data['shipping_address']['country_code'] ) ) {
$shipping_country_code = strtoupper( $data['shipping_address']['country_code'] );
} elseif ( ! empty( $parsed['SHIPPING_COUNTRY'] ) ) {
// If the base token already has a country, use that.
// The original hook implementation might not expose SHIPPING_COUNTRY directly here.
// This is a hypothetical scenario.
$shipping_country_code = strtoupper( $parsed['SHIPPING_COUNTRY'] );
}
// Add the custom token to the parsed array if we found a country code.
if ( ! empty( $shipping_country_code ) ) {
$parsed['SHIPPING_COUNTRY_CODE'] = $shipping_country_code;
}
// Example: Modify an existing token. Let's say we want to append " (Shipping)" to the name.
if ( isset( $parsed['SHIPPING_NAME'] ) && ! empty( $parsed['SHIPPING_NAME'] ) ) {
$parsed['SHIPPING_NAME'] .= ' (Shipping)';
}
// Always return the modified 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:414
src/integrations/surecart/tokens/surecart-tokens-new-framework.php:569
public function hydrate_shipping_tokens( $parsed, $data ) {
$shipping_address = null;
if ( is_a( $data, 'SureCartModelsPurchase' ) && isset( $data->initial_order->checkout->shipping_address ) ) {
$shipping_address = $data->initial_order->checkout->shipping_address;
}
if ( is_a( $data, 'SureCartModelsCheckout' ) && isset( $data->shipping_address ) ) {
$shipping_address = $data->shipping_address;
}
if ( null === $shipping_address ) {
return $parsed;
}
$parsed = $parsed + array(
'SHIPPING_COUNTRY' => empty( $shipping_address->country ) ? '' : $shipping_address->country,
'SHIPPING_STATE' => empty( $shipping_address->state ) ? '' : $shipping_address->state,
'SHIPPING_CITY' => empty( $shipping_address->city ) ? '' : $shipping_address->city,
'SHIPPING_LINE_1' => empty( $shipping_address->line_1 ) ? '' : $shipping_address->line_1,
'SHIPPING_LINE_2' => empty( $shipping_address->line_2 ) ? '' : $shipping_address->line_2,
'SHIPPING_POSTCODE' => empty( $shipping_address->postal_code ) ? '' : $shipping_address->postal_code,
'SHIPPING_NAME' => empty( $shipping_address->name ) ? '' : $shipping_address->name,
);
return apply_filters( 'automator_surecart_hydrate_shipping_tokens', $parsed, $data );
}