Storefront drawer API

Browser JavaScript API for interacting with the Kaching Cart drawer from Shopify themes, page builders, and apps.

Cart docs
On this page

Kaching Cart is a Shopify app that adds a fully-featured cart drawer to a merchant's storefront. This page documents the storefront drawer API β€” a browser-side JavaScript object attached to window.kachingCartApi that lets themes, page builders, and third-party apps drive the drawer: open and close it, read cart state, refresh data, and override the default checkout button behavior.

Prerequisites

  • A Shopify store with the Kaching Cart app installed and enabled on the storefront
  • The cart extension loaded on the page β€” window.kachingCartApi is only available after Kaching Cart has booted

Accessing the API

Once the cart extension boots, the API is attached to the global window object:

javascript
window.kachingCartApi

In non-preview environments, opening the browser DevTools console will show a collapsed log group β€” πŸ›οΈ Kaching Cart API Documentation β€” listing every available method along with its arguments and whether it returns a Promise. This is generated from the same source as this page and is the canonical reference at runtime.

TypeScript

If you're working in TypeScript, declare the global so the compiler knows about it:

typescript
declare global {
  interface Window {
    kachingCartApi?: KachingCartApi;
  }
}

interface KachingCartApi {
  openCart: (options?: { refetchCart?: boolean }) => void;
  closeCart: () => void;
  refreshCart: () => Promise<void>;
  getCartData: () => CartData | undefined;
  updateTieredPromotionsBar: () => Promise<void>;
  getCheckoutButtonClickHandler: () =>
    | ((event: MouseEvent) => boolean | Promise<boolean>)
    | null;
  setCheckoutButtonClickHandler: (
    newHandler: (event: MouseEvent) => boolean | Promise<boolean>,
  ) => void;
}

kachingCartApi is typed as optional because it's only present after the extension boots β€” always narrow before calling.

Methods

openCart

Opens the cart drawer.

javascript
window.kachingCartApi.openCart();
typescript
openCart(options?: { refetchCart?: boolean }): void
OptionTypeDefaultDescription
refetchCartbooleanfalseWhen true, the drawer refetches cart data as it opens. Use this after adding/updating items outside the drawer.

Example β€” open after adding an item via the Shopify AJAX API:

javascript
await fetch("/cart/add.js", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ id: variantId, quantity: 1 }),
});

window.kachingCartApi?.openCart({ refetchCart: true });

closeCart

Closes the cart drawer.

javascript
window.kachingCartApi.closeCart();
typescript
closeCart(): void

refreshCart

Refetches the cart data from Shopify and updates the drawer. Use after a mutation that happened outside Kaching Cart's own controls β€” for example, an Add-to-Cart elsewhere on the page, or a third-party app that mutated the cart.

javascript
await window.kachingCartApi.refreshCart();
typescript
refreshCart(): Promise<void>

The returned Promise resolves once the refetch completes. If you need the drawer to reopen with the fresh data, prefer `openCart({ refetchCart: true })` β€” it combines both operations.


getCartData

Returns the currently cached cart data synchronously. The shape follows Shopify's Cart AJAX API response, with a few Kaching-specific extensions.

javascript
const cart = window.kachingCartApi.getCartData();

if (cart) {
  console.log(`Cart has ${cart.item_count} items, total ${cart.total_price}`);
}
typescript
getCartData(): CartData | undefined

Returns undefined if the cart hasn't been fetched yet. Commonly used fields:

FieldTypeDescription
tokenstringShopify cart token.
item_countnumberTotal quantity across all line items.
total_pricenumberCart total in the store's currency subunits (e.g. cents).
total_discountnumberTotal discount applied to the cart, in subunits.
items_subtotal_pricenumberSubtotal before discounts, in subunits.
currencystringISO currency code, e.g. "USD".
itemsarrayLine items in the cart.
attributesobjectCart-level custom attributes.
notestringCart note.
checkoutUrlstring?Resolved checkout URL when available.

updateTieredPromotionsBar

Recomputes the tiered promotions bar and applies any free gifts, automatic discounts, or upgrades that have become eligible. Useful when you're about to redirect a customer to checkout after adding an item programmatically and want their cart-level promotions reflected before they leave the storefront.

javascript
await window.kachingCartApi.updateTieredPromotionsBar();
typescript
updateTieredPromotionsBar(): Promise<void>

If the tiered promotions bar is not active on the store, this is a no-op. Otherwise it fetches the latest cart state with promo operations resolved, then resumes normal cart interception.

Example β€” add an item then go straight to checkout with promos applied:

javascript
await fetch("/cart/add.js", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ id: variantId, quantity: 1 }),
});

await window.kachingCartApi?.updateTieredPromotionsBar();
window.location.href = "/checkout";

getCheckoutButtonClickHandler

Returns the currently registered custom checkout button handler, or null if none has been set.

javascript
const handler = window.kachingCartApi.getCheckoutButtonClickHandler();
typescript
getCheckoutButtonClickHandler():
  | ((event: MouseEvent) => boolean | Promise<boolean>)
  | null

Use this if you want to compose with an existing handler β€” read it, wrap it, and pass the wrapped version back via `setCheckoutButtonClickHandler`.


setCheckoutButtonClickHandler

Overrides the default checkout button click handler with a function of your own. The handler runs when the customer clicks the checkout button inside the cart drawer.

javascript
window.kachingCartApi.setCheckoutButtonClickHandler((event) => {
  // run custom logic β€” analytics, validation, custom redirect, etc.
  return true; // allow the default redirect to proceed
});
typescript
setCheckoutButtonClickHandler(
  newHandler: (event: MouseEvent) => boolean | Promise<boolean>,
): void

Return value semantics:

Returned valueBehavior
trueContinue with the default checkout redirect.
falseCancel the default redirect.
Promise<boolean>Awaited β€” same semantics as the resolved value.
Anything elseTreated as "continue" β€” only false cancels.

Example β€” redirect to a custom checkout page:

javascript
window.kachingCartApi.setCheckoutButtonClickHandler((event) => {
  event.preventDefault();
  window.location.href = "/pages/my-custom-checkout";
  return false; // cancel the default redirect
});

Example β€” run async validation before allowing checkout:

javascript
window.kachingCartApi.setCheckoutButtonClickHandler(async (event) => {
  const ok = await validateCartWithMyBackend();

  if (!ok) {
    alert("Please resolve the issues in your cart before checking out.");
    return false;
  }

  return true;
});

Example β€” compose with an existing handler:

javascript
const previous = window.kachingCartApi.getCheckoutButtonClickHandler();

window.kachingCartApi.setCheckoutButtonClickHandler(async (event) => {
  trackCheckoutClick();

  if (previous) {
    return await previous(event);
  }

  return true;
});

Can’t find what you need?

Ask the Kaching team about your integration in chat.