Search.../

addToCurrentCart( )

Adds catalog line items to the current site visitor's cart.

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