Skip to content

PHP Filters

The primary extension hook. Add arbitrary structured data to the checkout session. Data is stored in the session, available in the checkout frontend as window.__SWFT_SESSION__.extensions, and written to the WooCommerce order as _swft_ext_{key} meta on payment completion.

apply_filters( 'swft_session_extensions', array $extensions, WC_Cart $cart ): array

Parameters:

ParameterTypeDescription
$extensionsarrayCurrent extensions object. Initially empty []. May already contain keys from other plugins.
$cartWC_CartThe current WooCommerce cart object.

Return: array — the modified extensions array. Keys must be strings. Values may be any JSON-serialisable type.

Example:

add_filter( 'swft_session_extensions', function( array $extensions, WC_Cart $cart ): array {
$extensions['gift_options'] = [
'message' => WC()->session->get( 'gift_message' ) ?? '',
'wrap' => (bool) WC()->session->get( 'gift_wrap' ),
];
return $extensions;
}, 10, 2 );

Data flow:

WP plugin → swft_session_extensions filter
→ payload['extensions']['gift_options'] = { message: '...', wrap: true }
→ POST api.swft.co.uk/sessions
→ stored in Supabase + Cloudflare KV
→ window.__SWFT_SESSION__.extensions.gift_options
→ WooCommerce order meta: _swft_ext_gift_options = '{"message":"...","wrap":true}'

Modify or exclude a single line item before it is added to the session payload. Return null or false to exclude the item (e.g. if your plugin represents it differently in the extensions object).

apply_filters( 'swft_session_line_item', array $line_item, array $cart_item, WC_Product $product, string $cart_item_key ): array|null|false

Parameters:

ParameterTypeDescription
$line_itemarrayThe built line item. See structure below.
$cart_itemarrayRaw WooCommerce cart item data from WC()->cart->get_cart().
$productWC_ProductThe product object.
$cart_item_keystringThe cart item key.

Line item structure:

[
'product_id' => int,
'variation_id' => int|null,
'name' => string,
'quantity' => int,
'price' => int, // minor units (pence)
'subtotal' => int, // minor units
'total' => int, // minor units
'tax' => int, // minor units
'image_url' => string|null,
'sku' => string|null,
'attributes' => object, // variation attributes as key-value pairs
]

Example — add custom meta to a line item:

add_filter( 'swft_session_line_item', function( array $item, array $cart_item, WC_Product $product ): array {
$item['engraving_text'] = $cart_item['engraving_text'] ?? '';
return $item;
}, 10, 3 );

Example — exclude a virtual line item:

add_filter( 'swft_session_line_item', function( $item, array $cart_item, WC_Product $product ) {
if ( $product->is_virtual() && $product->get_sku() === 'GIFT_WRAP_FEE' ) {
return null; // exclude from line items — handled via extensions instead
}
return $item;
}, 10, 3 );

Modify all line items after the loop, before they are added to the payload.

apply_filters( 'swft_session_line_items', array $line_items, WC_Cart $cart ): array

Parameters:

ParameterTypeDescription
$line_itemsarrayArray of all built line item arrays.
$cartWC_CartThe current cart.

Example — sort line items by name:

add_filter( 'swft_session_line_items', function( array $items ): array {
usort( $items, fn( $a, $b ) => strcmp( $a['name'], $b['name'] ) );
return $items;
} );

Modify the available shipping methods passed to the checkout.

apply_filters( 'swft_session_shipping_methods', array $shipping_methods, WC_Cart $cart ): array

Parameters:

ParameterTypeDescription
$shipping_methodsarrayArray of shipping method arrays. Each has id, name, price (minor units), description.
$cartWC_CartThe current cart.

Example — add a custom shipping option:

add_filter( 'swft_session_shipping_methods', function( array $methods, WC_Cart $cart ): array {
if ( $cart->get_subtotal() >= 50 ) {
$methods[] = [
'id' => 'click_collect',
'name' => 'Click & Collect',
'price' => 0,
'description' => 'Collect from our store in 2 hours',
];
}
return $methods;
}, 10, 2 );

Modify the pre-filled customer data. Used to pre-fill the details step for logged-in customers.

apply_filters( 'swft_session_customer', array $customer, WC_Customer $wc_customer ): array

Parameters:

ParameterTypeDescription
$customerarray`[‘email’ => string
$wc_customerWC_CustomerThe WooCommerce customer object.

Example:

add_filter( 'swft_session_customer', function( array $customer, WC_Customer $wc_customer ): array {
// Add phone number for pre-fill
$customer['phone'] = $wc_customer->get_billing_phone() ?: null;
return $customer;
}, 10, 2 );

Modify the totals sent to the Swft API. All values are integers in minor units (pence/cents).

apply_filters( 'swft_session_totals', array $totals, WC_Cart $cart ): array

Parameters:

ParameterTypeDescription
$totalsarray['subtotal' => int, 'tax' => int, 'discount' => int] — all in minor units.
$cartWC_CartThe current cart.

Last-resort filter. Modify the complete payload before it is sent to the Swft API. Use specific filters above where possible.

apply_filters( 'swft_session_payload', array $payload ): array

Payload structure:

[
'merchant_api_key' => string,
'cart_hash' => string,
'currency' => string, // ISO 4217 (e.g. 'GBP')
'line_items' => array,
'shipping_methods' => array,
'subtotal' => int, // minor units
'tax' => int,
'discount' => int,
'customer' => array,
'extensions' => array,
'meta' => [
'store_url' => string,
'needs_shipping' => bool,
'needs_payment' => bool,
'wc_version' => string,
'plugin_version' => string,
],
]

Modify the full cart object before it is rendered in the cart drawer.

apply_filters( 'swftcart_cart_data', array $cart_data ): array

Modify a single cart item before rendering.

apply_filters( 'swftcart_cart_item', array $item, string $cart_item_key ): array

Override the enabled/disabled state of modules.

apply_filters( 'swftcart_modules', array $modules ): array

$modules is an associative array of option_name => bool. See Modules for all option names.


Override any UI string in the cart drawer.

apply_filters( 'swftcart_i18n', array $strings ): array

Example:

add_filter( 'swftcart_i18n', function( array $strings ): array {
$strings['checkout_button'] = 'Proceed to secure checkout';
$strings['empty_cart'] = 'Your bag is empty';
return $strings;
} );

Set the content of the announcement bar module.

apply_filters( 'swftcart_announcements', array $announcements ): array

Each announcement is an array with text (string) and optionally url (string) and icon (string).

Example:

add_filter( 'swftcart_announcements', function(): array {
return [
[
'text' => 'Free delivery on orders over £50',
'icon' => 'truck',
],
[
'text' => '10% off your first order — use WELCOME10',
'url' => '/shop/',
],
];
} );

Override CSS custom property values for the cart drawer theme.

apply_filters( 'swftcart_theme_vars', array $vars ): array

See Themes for the full variable list.


Set the data for the delivery countdown module.

apply_filters( 'swftcart_delivery_data', array $data ): array

Structure:

[
'cutoff_time' => '14:00', // 24h time — order by this time for same-day
'timezone' => 'Europe/London',
'message_before' => 'Order in {time} for same-day dispatch',
'message_after' => 'Order now for next-day dispatch',
]

Set the product IDs shown in the upsell module.

apply_filters( 'swftcart_upsell_ids', array $ids ): array

Example — return bestsellers dynamically:

add_filter( 'swftcart_upsell_ids', function( array $ids ): array {
$bestsellers = wc_get_products( [
'limit' => 6,
'orderby' => 'popularity',
'order' => 'DESC',
'status' => 'publish',
] );
return array_map( fn( $p ) => $p->get_id(), $bestsellers );
} );