Add To Cart

amerta


amerta / Cart / addToCart

Function: addToCart()

addToCart(product, quantity, variantOptions, locale): Promise<{ cart: CartWithCalculations; error?: null; } | { cart?: null; error: ApiError; }>

Defined in: amerta/theme/utilities/cart.client.ts:16

Adds a product to the cart or increments its quantity.

Parameters

| Parameter | Type | Description | | ------ | ------ | ------ | | product | { id: string; price: number; title: string; } | The product object containing id, title, and price. | | product.id | string | - | | product.price | number | - | | product.title | string | - | | quantity | number | The quantity to add. | | variantOptions | { option: string; value: string; }[] | Optional array of variant option objects. | | locale | string | - |

Returns

Promise<{ cart: CartWithCalculations; error?: null; } | { cart?: null; error: ApiError; }>

The updated cart or an error object.

Example

const res = await addToCart({ id: "123", title: "Shirt", price: 20 }, 2, [], "en-US");