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:
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
Change a quantity
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.Render a cart
The basket holds IDs, so join them withstore.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.isOutOfStockis true - Quantity limits - keep quantities between
minimumQuantityandmaximumQuantity - Subscriptions - a subscription can’t share an order with one-time products
Where the basket is kept
Advanced
These are rarely needed, but they’re part of the API.Backups
checkout() never changes the basket.
Merge
Move
false
when the source line isn’t there.
Integrity and stats
Next
Checkout
Turning the basket into an order
Products
Stock, limits and subscriptions