PHP Filters
Swft Checkout filters
Section titled “Swft Checkout filters”swft_session_extensions
Section titled “swft_session_extensions”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 ): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$extensions | array | Current extensions object. Initially empty []. May already contain keys from other plugins. |
$cart | WC_Cart | The 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}'swft_session_line_item
Section titled “swft_session_line_item”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|falseParameters:
| Parameter | Type | Description |
|---|---|---|
$line_item | array | The built line item. See structure below. |
$cart_item | array | Raw WooCommerce cart item data from WC()->cart->get_cart(). |
$product | WC_Product | The product object. |
$cart_item_key | string | The 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 );swft_session_line_items
Section titled “swft_session_line_items”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 ): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$line_items | array | Array of all built line item arrays. |
$cart | WC_Cart | The 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;} );swft_session_shipping_methods
Section titled “swft_session_shipping_methods”Modify the available shipping methods passed to the checkout.
apply_filters( 'swft_session_shipping_methods', array $shipping_methods, WC_Cart $cart ): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$shipping_methods | array | Array of shipping method arrays. Each has id, name, price (minor units), description. |
$cart | WC_Cart | The 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 );swft_session_customer
Section titled “swft_session_customer”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 ): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$customer | array | `[‘email’ => string |
$wc_customer | WC_Customer | The 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 );swft_session_totals
Section titled “swft_session_totals”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 ): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$totals | array | ['subtotal' => int, 'tax' => int, 'discount' => int] — all in minor units. |
$cart | WC_Cart | The current cart. |
swft_session_payload
Section titled “swft_session_payload”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 ): arrayPayload 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, ],]Swft Cart filters
Section titled “Swft Cart filters”swftcart_cart_data
Section titled “swftcart_cart_data”Modify the full cart object before it is rendered in the cart drawer.
apply_filters( 'swftcart_cart_data', array $cart_data ): arrayswftcart_cart_item
Section titled “swftcart_cart_item”Modify a single cart item before rendering.
apply_filters( 'swftcart_cart_item', array $item, string $cart_item_key ): arrayswftcart_modules
Section titled “swftcart_modules”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.
swftcart_i18n
Section titled “swftcart_i18n”Override any UI string in the cart drawer.
apply_filters( 'swftcart_i18n', array $strings ): arrayExample:
add_filter( 'swftcart_i18n', function( array $strings ): array { $strings['checkout_button'] = 'Proceed to secure checkout'; $strings['empty_cart'] = 'Your bag is empty'; return $strings;} );swftcart_announcements
Section titled “swftcart_announcements”Set the content of the announcement bar module.
apply_filters( 'swftcart_announcements', array $announcements ): arrayEach 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/', ], ];} );swftcart_theme_vars
Section titled “swftcart_theme_vars”Override CSS custom property values for the cart drawer theme.
apply_filters( 'swftcart_theme_vars', array $vars ): arraySee Themes for the full variable list.
swftcart_delivery_data
Section titled “swftcart_delivery_data”Set the data for the delivery countdown module.
apply_filters( 'swftcart_delivery_data', array $data ): arrayStructure:
[ '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',]swftcart_upsell_ids
Section titled “swftcart_upsell_ids”Set the product IDs shown in the upsell module.
apply_filters( 'swftcart_upsell_ids', array $ids ): arrayExample — 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 );} );