Cart JavaScript SDK
Listen to cart events, open, close and refresh the drawer, read the cart, and override checkout with the global optimaio object. No install needed.
The Zupli cart exposes a small JavaScript SDK on every storefront page as the global optimaio object. Use it to react to the cart, control the drawer and customise checkout. There’s nothing to install.
Add your code in JavaScript overrides (Appearance > Advanced settings), or in your theme. See Custom CSS and JavaScript.
Quick start
optimaio.on('open', ({ cart }) => {
console.log('Cart opened', cart);
});
optimaio.do('openCart');
Events
Listen with optimaio.on(eventName, callback).
| Event | Fires when | Callback receives |
|---|---|---|
open |
The drawer opens | { cart } |
close |
The drawer closes | { cart } |
checkoutAttempt |
The checkout button is clicked, after any required terms checkbox passes | { cart, discountCodes } |
cart:updated |
The cart changes | The cart object itself |
cart:rendered |
The drawer finishes drawing the cart | The cart object itself |
cart:refresh |
Zupli refreshes the cart, for example after adding a gift | Nothing |
gifts:updated |
The gifts in the cart change | The gift state |
cart:cleared |
A scarcity timer set to Clear cart empties the cart | { source: 'timer' } |
appearance-assigned |
A shopper is given an A/B test variant | { variantId, campaignId } |
cart is the current Shopify cart object; discountCodes are the codes the shopper entered (Zupli’s own rewards are automatic discounts, so they’re not in this list).
optimaio.on('checkoutAttempt', ({ cart, discountCodes }) => {
console.log('Checkout clicked', cart.total_price, discountCodes);
});
Actions
Control the drawer with optimaio.do(actionName).
| Action | What it does |
|---|---|
openCart |
Opens the drawer |
closeCart |
Closes the drawer |
refreshCart |
Fetches the cart from Shopify again and updates the drawer |
Getters
Read state with optimaio.get(key).
| Key | Returns |
|---|---|
cartInfo |
The latest Shopify cart object, or null before the cart has loaded |
isOpen |
true if the drawer is open |
const cart = optimaio.get('cartInfo');
console.log(cart.total_price, cart.items);
if (!optimaio.get('isOpen')) optimaio.do('openCart');
Checkout override
optimaio.overrideCheckout(callback) replaces what the checkout button does. Your function receives the cart and the discount codes.
optimaio.overrideCheckout((cart, discountCodes) => {
// Shopify cart prices are in minor units: 2000 is ₹20.00
if (cart.total_price < 2000) {
alert('Minimum order value is ₹20');
return;
}
window.location.href = '/checkout';
});
- It only replaces the Zupli drawer’s checkout button, not your theme’s checkout buttons or Shop Pay.
- Only one override is kept; registering another replaces it.
- Zupli records Checkout Started for analytics before your function runs, so you don’t need to.
- If your function throws an error straight away, the shopper is sent to
/checkout. Keep it synchronous so that fallback works.
To hand over to GoKwik, Razorpay or another provider, see Use a third-party checkout.
Wait for the SDK
If your code can run before Zupli loads, wait for the page and check the object exists:
document.addEventListener('DOMContentLoaded', () => {
if (!window.optimaio) return;
optimaio.on('open', () => {
// ...
});
});
Summary
| Type | Name | Description |
|---|---|---|
| Event | open |
Drawer opened |
| Event | close |
Drawer closed |
| Event | checkoutAttempt |
Checkout button clicked |
| Event | cart:updated, cart:rendered, cart:refresh, gifts:updated, cart:cleared, appearance-assigned |
Cart, gift, timer and A/B changes |
| Action | openCart |
Open the drawer |
| Action | closeCart |
Close the drawer |
| Action | refreshCart |
Refetch the cart |
| Getter | cartInfo |
Current cart object |
| Getter | isOpen |
Drawer state |
| Override | overrideCheckout() |
Custom checkout behaviour |
Was this page helpful?
Thanks for letting us know.
Sorry about that. Tell us what was missing and we'll fix the page.