Skip to main content

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 of BasketItem 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

Returns true if backup was created successfully.

Restore Backup

Returns true if backup was restored successfully.

Example

Validation

Validate that the basket data is intact and properly formatted.
Returns 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.
Parameters:
  • otherBasket - Array of BasketItem objects to merge
Returns: The merged basket as an array of BasketItem objects.

Move Basket Item

Move a specific item from one product/variant to another.
Parameters:
  • fromProductId - Source product ID
  • fromVariantId - Source variant ID
  • toProductId - Destination product ID
  • toVariantId - Destination variant ID
Returns: true if the item was moved successfully.

Get Basket Statistics

Get detailed statistics about the current basket.
Returns: An object containing basket statistics (structure varies).

Complete Example

Here’s a complete shopping cart implementation:
Basket data is stored in localStorage - it’s not synced across devices or browsers

Next Steps

Products

Learn how to fetch and display products

Checkout

Process orders and handle payments
Last modified on November 27, 2025