Search.../

addToCart( )

Adds catalog line items to a cart.

Description

The addToCart() function returns a Promise that resolves to the updated cart when the specified items have been added.

Note: When adding catalog items, options.lineItems.catalogReference is required.

Syntax

function addToCart(_id: string, options: AddToCartOptions): Promise<AddToCartResponse>

addToCart Parameters

NAME
TYPE
DESCRIPTION
_id
string

Cart ID.

options
Optional
AddToCartOptions

Items to be added to cart.

Returns

Fulfilled - Cart.

Return Type:

Promise<
AddToCartResponse
>
NAME
TYPE
DESCRIPTION
cart
Cart

Updated cart.

Was this helpful?

Add a catalog item to a cart

Copy Code
1/**************************************
2 * Backend code - my-backend-file.jsw *
3 *************************************/
4
5import { cart } from 'wix-ecom-backend';
6
7export async function myAddToCartFunction(_id, options) {
8 try {
9 const updatedCart = await cart.addToCart(_id, options);
10 console.log('Success! Updated cart:', updatedCart);
11 return updatedCart;
12 } catch (error) {
13 console.error(error);
14 // Handle the error
15 }
16}
17
18/*************
19 * Page code *
20 ************/
21
22import { myAddToCartFunction } from 'backend/my-backend-file';
23
24// Sample addToCart function parameters:
25const cartId = '96a61a4b-6b61-47d1-a039-0213a8230ccd';
26const options = {
27 "lineItems": [{
28 "catalogReference": {
29 // Wix Stores appId
30 "appId": "1380b703-ce81-ff05-f115-39571d94dfcd",
31 // Wix Stores productId
32 "catalogItemId": "c8539b66-7a44-fe18-affc-afec4be8562a"
33 },
34 "quantity": 1
35 }]
36};
37
38myAddToCartFunction(cartId, options)
39 .then((updatedCart) => {
40 const cartSubtotal = updatedCart.subtotal.amount;
41 const cartCheckoutId = updatedCart.checkoutId;
42 const numberOfCartItems = updatedCart.lineItems.length;
43
44 console.log('Success! Updated cart:', updatedCart);
45 return updatedCart;
46 })
47 .catch((error) => {
48 console.error(error);
49 // Handle the error
50 });
51
52/* Promise resolves to:
53 *
54 * {
55 * "_id": "ba47a627-7bb8-4918-89b2-6a72af464765",
56 * "lineItems": [
57 * {
58 * "_id": "00000000-0000-0000-0000-000000000001",
59 * "quantity": 1,
60 * "catalogReference": {
61 * "catalogItemId": "c8539b66-7a44-fe18-affc-afec4be8562a",
62 * "appId": "1380b703-ce81-ff05-f115-39571d94dfcd"
63 * },
64 * "productName": {
65 * "original": "Shirt",
66 * "translated": "Shirt"
67 * },
68 * "url": "https://example.wixsite.com",
69 * "price": {
70 * "amount": "10",
71 * "convertedAmount": "10",
72 * "formattedAmount": "€10.00",
73 * "formattedConvertedAmount": "€10.00"
74 * },
75 * "fullPrice": {
76 * "amount": "10",
77 * "convertedAmount": "10",
78 * "formattedAmount": "€10.00",
79 * "formattedConvertedAmount": "€10.00"
80 * },
81 * "priceBeforeDiscounts": {
82 * "amount": "10",
83 * "convertedAmount": "10",
84 * "formattedAmount": "€10.00",
85 * "formattedConvertedAmount": "€10.00"
86 * },
87 * "descriptionLines": [],
88 * "image": "wix:image://v1/3c76e2_c5331f937348492a97df87b0a3b34ea4~mv2.jpg#originWidth=1000&originHeight=1000",
89 * "availability": {
90 * "status": "AVAILABLE"
91 * },
92 * "physicalProperties": {
93 * "sku": "364115376135191",
94 * "shippable": true
95 * },
96 * "couponScopes": [
97 * {
98 * "namespace": "stores",
99 * "group": {
100 * "name": "collection",
101 * "entityId": "00000000-000000-000000-000000000001"
102 * }
103 * },
104 * {
105 * "namespace": "stores",
106 * "group": {
107 * "name": "product",
108 * "entityId": "c8539b66-7a44-fe18-affc-afec4be8562a"
109 * }
110 * }
111 * ],
112 * "itemType": {
113 * "preset": "PHYSICAL"
114 * },
115 * "paymentOption": "FULL_PAYMENT_ONLINE"
116 * }
117 * ],
118 * "buyerInfo": {
119 * "visitorId": "4c7ce95c-9fb3-417d-9f02-b41e82b841f7"
120 * },
121 * "currency": "EUR",
122 * "conversionCurrency": "EUR",
123 * "buyerLanguage": "en",
124 * "siteLanguage": "en",
125 * "taxIncludedInPrices": false,
126 * "weightUnit": "KG",
127 * "subtotal": {
128 * "amount": "10",
129 * "convertedAmount": "10",
130 * "formattedAmount": "€10.00",
131 * "formattedConvertedAmount": "€10.00"
132 * },
133 * "appliedDiscounts": [],
134 * "inSync": true,
135 * "_createdDate": "2022-05-15T11:31:30.484Z",
136 * "_updatedDate": "2022-05-23T12:11:55.095Z"
137 * }
138 *
139 */
140