Search.../

addToCurrentCart( )

Adds line items to the current site visitor's cart. Both catalog line items and custom line items are supported.

Description

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

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

Syntax

function addToCurrentCart(options: AddToCurrentCartOptions): Promise<AddToCartResponse>

addToCurrentCart Parameters

NAME
TYPE
DESCRIPTION
options
Optional
AddToCurrentCartOptions

Items to be added to the current cart.

Returns

Return Type:

Promise<
AddToCartResponse
>
NAME
TYPE
DESCRIPTION
cart
Cart

Updated cart.

Was this helpful?

Add a Wix Stores product to the current cart

Copy Code
1/**************************************
2 * Backend code - my-backend-file.jsw *
3 **************************************/
4
5import { currentCart } from 'wix-ecom-backend';
6
7export async function myAddToCurrentCartFunction(options) {
8 try {
9 const updatedCurrentCart = await currentCart.addToCurrentCart(options);
10 console.log('Success! Updated current cart:', updatedCurrentCart);
11 return updatedCurrentCart;
12 } catch (error) {
13 console.error(error);
14 // Handle the error
15 }
16}
17
18/*************
19 * Page code *
20 ************/
21
22import { myAddToCurrentCartFunction } from 'backend/my-backend-file';
23
24// Sample options object:
25const options = {
26 "lineItems": [{
27 "catalogReference": {
28 // Wix Stores appId
29 "appId": "1380b703-ce81-ff05-f115-39571d94dfcd",
30 // Wix Stores productId
31 "catalogItemId": "1a2d7e83-4bef-31d5-09e1-3326ee271c09",
32 "options": {
33 // Wix Stores variantId
34 "variantId": "132b84e8-aab8-47a1-a1f6-2c47557b64a4"
35 }
36 },
37 "quantity": 1
38 }]
39};
40
41myAddToCurrentCartFunction(options)
42 .then((updatedCurrentCart) => {
43 const cartId = updatedCurrentCart._id;
44 const numberOfCartLineItems = updatedCurrentCart.lineItems.length;
45
46 console.log('Success! Updated cart:', updatedCurrentCart);
47 return updatedCurrentCart;
48 })
49 .catch((error) => {
50 console.error(error);
51 // Handle the error
52 });
53
54/* Promise resolves to:
55 *
56 * {
57 * "_id": "e4156539-32b8-48dd-97f8-164b5a5b8740",
58 * "lineItems": [
59 * {
60 * "_id": "00000000-0000-0000-0000-000000000001",
61 * "quantity": 1,
62 * "catalogReference": {
63 * "catalogItemId": "1a2d7e83-4bef-31d5-09e1-3326ee271c09",
64 * "appId": "1380b703-ce81-ff05-f115-39571d94dfcd",
65 * "options": {
66 * "variantId": "132b84e8-aab8-47a1-a1f6-2c47557b64a4"
67 * }
68 * },
69 * "productName": {
70 * "original": "Watch",
71 * "translated": "Watch"
72 * },
73 * "url": "https://example.wixsite.com",
74 * "price": {
75 * "amount": "30",
76 * "convertedAmount": "30",
77 * "formattedAmount": "€30.00",
78 * "formattedConvertedAmount": "€30.00"
79 * },
80 * "fullPrice": {
81 * "amount": "30",
82 * "convertedAmount": "30",
83 * "formattedAmount": "€30.00",
84 * "formattedConvertedAmount": "€30.00"
85 * },
86 * "priceBeforeDiscounts": {
87 * "amount": "30",
88 * "convertedAmount": "30",
89 * "formattedAmount": "€30.00",
90 * "formattedConvertedAmount": "€30.00"
91 * },
92 * "descriptionLines": [
93 * {
94 * "name": {
95 * "original": "Size",
96 * "translated": "Size"
97 * },
98 * "plainText": {
99 * "original": "Medium",
100 * "translated": "Medium"
101 * },
102 * "lineType": "UNRECOGNISED"
103 * },
104 * {
105 * "name": {
106 * "original": "Color",
107 * "translated": "Color"
108 * },
109 * "colorInfo": {
110 * "original": "Grey",
111 * "translated": "Grey",
112 * "code": "rgb(128, 128, 128)"
113 * },
114 * "lineType": "UNRECOGNISED"
115 * }
116 * ],
117 * "image": "wix:image://v1/3c76e2_8891bbe3372a428aac976ac59aa0ac74~mv2.jpg#originWidth=1000&originHeight=1000",
118 * "availability": {
119 * "status": "AVAILABLE"
120 * },
121 * "physicalProperties": {
122 * "sku": "217537123517253",
123 * "shippable": true
124 * },
125 * "couponScopes": [
126 * {
127 * "namespace": "stores",
128 * "group": {
129 * "name": "collection",
130 * "entityId": "00000000-000000-000000-000000000001"
131 * }
132 * },
133 * {
134 * "namespace": "stores",
135 * "group": {
136 * "name": "product",
137 * "entityId": "1a2d7e83-4bef-31d5-09e1-3326ee271c09"
138 * }
139 * }
140 * ],
141 * "itemType": {
142 * "preset": "PHYSICAL"
143 * },
144 * "paymentOption": "FULL_PAYMENT_ONLINE"
145 * }
146 * ],
147 * "buyerInfo": {
148 * "memberId": "c43190d2-eea3-493e-b6e8-f146850c6873"
149 * },
150 * "currency": "EUR",
151 * "conversionCurrency": "EUR",
152 * "buyerLanguage": "en",
153 * "siteLanguage": "en",
154 * "taxIncludedInPrices": false,
155 * "weightUnit": "KG",
156 * "subtotal": {
157 * "amount": "30",
158 * "convertedAmount": "30",
159 * "formattedAmount": "€30.00",
160 * "formattedConvertedAmount": "€30.00"
161 * },
162 * "appliedDiscounts": [],
163 * "inSync": false,
164 * "_createdDate": "2022-05-23T13:17:46.801Z",
165 * "_updatedDate": "2022-05-23T13:17:46.801Z"
166 * }
167 *
168 */
169