Skip to content

Public API — window.SwftCart

window.SwftCart is available on all pages where the Swft Cart plugin is active. It is initialised synchronously on DOM ready — no event to wait for.

MethodSignatureDescription
open() => voidOpen the cart drawer
close() => voidClose the cart drawer
refresh() => Promise<void>Reload cart data from WooCommerce and re-render
toast(msg: string, label?: string, fn?: () => void) => voidShow a toast notification. Optional label and fn add an action button.
on(event: string, fn: (detail: unknown) => void) => voidSubscribe to a swftcart:* event
off(event: string, fn: (detail: unknown) => void) => voidUnsubscribe from a swftcart:* event
PropertyTypeDescription
configobjectRead-only PHP configuration object. Contains all enabled module flags, theme variables, and i18n strings as output by the PHP plugin.
document.querySelectorAll('[data-open-cart]').forEach(btn => {
btn.addEventListener('click', () => SwftCart.open())
})

Show a toast when a product is saved for later

Section titled “Show a toast when a product is saved for later”
SwftCart.on('item-saved-for-later', (detail) => {
SwftCart.toast('Saved for later', 'View saved items', () => {
// scroll to saved items section
})
})
fetch('/wp-admin/admin-ajax.php', {
method: 'POST',
body: new URLSearchParams({ action: 'my_custom_action' }),
}).then(() => SwftCart.refresh())
SwftCart.on('checkout', (e) => {
if (!document.getElementById('terms-accepted').checked) {
e.preventDefault()
SwftCart.toast('Please accept the terms and conditions before continuing.')
}
})

Note: the checkout event (fired as swftcart:checkout) is cancelable. Call e.preventDefault() to stop the redirect to checkout.

console.log(SwftCart.config.modules) // { swftcart_module_upsells: true, ... }
console.log(SwftCart.config.theme) // 'dark'
console.log(SwftCart.config.currency) // 'GBP'