Overview
The basket (shopping cart) is stored in the browser’s localStorage and persists across page reloads. All basket operations are synchronous and update the local state immediately.The
komerza object is available globally after including the script. Call
komerza.init('your-store-id') before using any other methods.Adding Items
Add a product variant to the basket with a specified quantity.Parameters
string
required
The unique identifier of the product
string
required
The unique identifier of the product variant
number
How many items to add (defaults to 1 if not provided)
Example
Viewing Basket
Get all items currently in the basket.Returns
Returns an array ofBasketItem objects:
Example
Getting Item Count
Get the total number of items in the basket (sum of all quantities).Returns
Returns a number representing the total quantity of all items.Example
Removing Items
Remove a specific product variant from the basket entirely.Parameters
string
required
The product identifier to remove
string
required
The variant identifier to remove
Example
Clearing Basket
Remove all items from the basket at once.Example
Backup & Restore
Create backups of the basket state and restore them later. Useful for handling checkout errors or implementing “save for later” features.Create Backup
true if backup was created successfully.
Restore Backup
true if backup was restored successfully.
Example
Validation
Validate that the basket data is intact and properly formatted.true if basket data is valid, false if corrupted.
Example
Advanced Operations
Merge Baskets
Merge items from another basket into the current basket. Useful for syncing carts across devices or sessions.otherBasket- Array ofBasketItemobjects to merge
BasketItem objects.
Move Basket Item
Move a specific item from one product/variant to another.fromProductId- Source product IDfromVariantId- Source variant IDtoProductId- Destination product IDtoVariantId- Destination variant ID
true if the item was moved successfully.
Get Basket Statistics
Get detailed statistics about the current basket.Complete Example
Here’s a complete shopping cart implementation:Next Steps
Products
Learn how to fetch and display products
Checkout
Process orders and handle payments