Cart API
Manage shopping carts, add items, and update quantities.
Create Cart
Initialize a new cart session, optionally with starting items.
Initialize empty cart
const cart = await swat.cart.create([]);
// Store cart.id in localStorage for persistenceInitialize with items
const cart = await swat.cart.create([
{ productId: 'prod_123', quantity: 1 },
{ productId: 'prod_456', quantity: 2 }
]);Get Cart
Retrieve current cart state. Useful on page load.
const cartId = localStorage.getItem('cart_id');
if (cartId) {
const cart = await swat.cart.get(cartId);
}Add Items
Add one or more products to an existing cart.
const updatedCart = await swat.cart.addItems(cartId, [
{ productId: 'prod_789', quantity: 1 }
]);Update Quantity
// Set quantity to 5
const updatedCart = await swat.cart.updateItem(cartId, 'prod_123', 5);Remove Item
const updatedCart = await swat.cart.removeItem(cartId, 'prod_123');