Skip to main content

How the basket works

The basket lives in the visitor’s browser (localStorage), so it survives reloads and is shared by every tab on your site. Every basket function is synchronous and takes effect immediately. It stores IDs and quantities, nothing else:
To show names, images and prices, look the items up in store.products - see Render a cart.

Add to the basket

Adding a variant that’s already in the basket increases its quantity rather than adding a second line.

Buy now

Empties the basket, then adds this one item. It doesn’t open checkout by itself - send the buyer straight on to your checkout form. It’s the simple way to sell a subscription, since those can’t share a cart with one-time products.

Change a quantity

Sets the quantity of an item that’s already in the basket. 0 or less removes it. An item that isn’t in the basket is ignored - use addToBasket() for that.

Remove and clear

Read the basket

React to changes

basketchange fires after every change to the basket, whether this page made it or another tab did. It doesn’t fire when nothing changed, such as removing an item that wasn’t there. It’s the event to build a cart badge or a cart drawer on.
The browser’s own storage event isn’t a substitute: it fires in other tabs, never in the one that made the change.
In React, subscribe in an effect and return the unsubscribe function as the cleanup:

Render a cart

The basket holds IDs, so join them with store.products - which includes unlisted products too, so a buyer who added one from a direct link still sees it. Don’t call getProduct() for each line: that’s a request per line, and each one records a product view in the store’s analytics.

What the basket doesn’t check

The basket accepts whatever you put in it. Checkout is where the rules are enforced, and it refuses an order that breaks one:
  • Stock - disable “Add to cart” when variant.isOutOfStock is true
  • Quantity limits - keep quantities between minimumQuantity and maximumQuantity
  • Subscriptions - a subscription can’t share an order with one-time products
Checking these in your UI means buyers find out while they shop, not when they pay.

Where the basket is kept

The basket belongs to the browser and your site’s origin. It isn’t synced across devices or browsers, and it isn’t tied to a store: if you develop several stores on the same localhost port, they share one basket. Call clearBasket() when you switch.

Advanced

These are rarely needed, but they’re part of the API.

Backups

The SDK saves a backup by itself whenever the page is hidden or closed. A restore only happens if the basket is empty or the backup is newer than it, so it can’t overwrite a basket the buyer has changed since. You don’t need a backup around checkout: checkout() never changes the basket.

Merge

Merges another list of items - one you saved on your own server, say - into the basket and saves the result. When both contain the same product and variant, the higher quantity wins; quantities aren’t added together. Lines without a product ID or with a quantity of 0 or less are skipped. Returns the basket after the merge.

Move

Moves a line’s whole quantity to another product or variant - handy when the buyer switches variant in the cart. If the destination is already in the basket, the quantities are combined. Returns false when the source line isn’t there.

Integrity and stats

Next

Checkout

Turning the basket into an order

Products

Stock, limits and subscriptions
Last modified on October 15, 2026