Home > widget-integration > CartService > addToCart
CartService.addToCart() method
Add product(s) to cart with validation and error handling
Supports both single product and multi-product additions. Handles cart count updates, post-add actions, and error scenarios.
Extension points: - Override to add custom validation rules - Override to customize post-add behavior - Override to customize error handling
Signature:
addToCart(item: AddToCartItem): Promise<AddToCartResult>;
Parameters
|
Parameter |
Type |
Description |
|---|---|---|
|
item |
Cart item configuration (single product or multi-product) |
Returns:
Promise<AddToCartResult>
Promise resolving to operation result with success status and optional response/error
Exceptions
{Error} When cart API operations fail or validation errors occur
Example 1
Add single product
const result = await cartService.addToCart({
productId: '12345',
quantity: 2,
properties: { gift_message: 'Happy Birthday!' }
});
if (result.success) {
console.log('Added to cart:', result.response);
} else {
console.error('Failed:', result.error);
}
Example 2
Add multiple products
const result = await cartService.addToCart({
items: [
{ id: '12345', quantity: 1 },
{ id: '67890', quantity: 2, selling_plan: 'sub_123' }
]
});