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.
Methods
Section titled “Methods”| Method | Signature | Description |
|---|---|---|
open | () => void | Open the cart drawer |
close | () => void | Close the cart drawer |
refresh | () => Promise<void> | Reload cart data from WooCommerce and re-render |
toast | (msg: string, label?: string, fn?: () => void) => void | Show a toast notification. Optional label and fn add an action button. |
on | (event: string, fn: (detail: unknown) => void) => void | Subscribe to a swftcart:* event |
off | (event: string, fn: (detail: unknown) => void) => void | Unsubscribe from a swftcart:* event |
Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
config | object | Read-only PHP configuration object. Contains all enabled module flags, theme variables, and i18n strings as output by the PHP plugin. |
Examples
Section titled “Examples”Open the cart from any button
Section titled “Open the cart from any button”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 })})Refresh cart after a custom AJAX action
Section titled “Refresh cart after a custom AJAX action”fetch('/wp-admin/admin-ajax.php', { method: 'POST', body: new URLSearchParams({ action: 'my_custom_action' }),}).then(() => SwftCart.refresh())Block checkout until a condition is met
Section titled “Block checkout until a condition is met”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.
Read config
Section titled “Read config”console.log(SwftCart.config.modules) // { swftcart_module_upsells: true, ... }console.log(SwftCart.config.theme) // 'dark'console.log(SwftCart.config.currency) // 'GBP'