Search.../

addProducts( )

Adds one or more products to the cart.

Description

The addProducts() function returns a Promise that is resolved when the specified products are added to the cart.

Use the quantity property of each AddToCartItem object that is passed to the products parameter to add one or more products to the cart at one time.

Use the options property of each AddToCartItem object that is passed to the products parameter to specify the product options to choose when adding the product to the cart. For example, if a product comes in different sizes, you can specify the size that should be added to the cart. If the product you are adding to the cart has options, you must specify which options should be chosen.

You can see a product's option information in the productOptions field in the Stores/Products collection.

You can use product.getOptionsAvailability() to determine if an item with specific options is available for purchase.

Note: If you use wix-stores-backend.createProduct() immediately before adding that product to the cart, we suggest setting a timeout for "1000" milliseconds (1 second) before calling cart.addProducts(). While this slows down the operation slightly, it also ensures proper retrieval of the newly created product before adding it to the cart.

Syntax

function addProducts(products: Array<AddToCartItem>): Promise<CartObj>

addProducts Parameters

NAME
TYPE
DESCRIPTION
products
Array<AddToCartItem>

One or more products to be added to the cart.

Returns

Fulfilled - The updated cart with added products.

Return Type:

Promise<CartObj>
NAME
TYPE
DESCRIPTION
_id
string

Unique identifier of the shopping cart.

appliedCoupon
CartAppliedCoupon

Coupon applied in the shopping cart.

billingAddress
CartAddress

Cart billing address.

buyerInfo
CartBuyerInfo

The buyer's information.

status
string

Cart status. Either "INCOMPLETE" or "COMPLETE".

currency
Currency

Currency of the shopping cart.

shippingInfo
CartShippingInfo

The shopping cart's shipping information.

lineItems
Array<CartLineItem>

Items in the shopping cart.

totals
OrderTotals

The shopping cart's totals.

weightUnit
string

The order's units of weight. One of: "KG", "LB", or "UNSPECIFIED_WEIGHT_UNIT".

Was this helpful?

Add products to the cart

Copy Code
1import { cart } from 'wix-stores-frontend';
2
3const products = [
4 {
5 "productId": "0873ed58-f88d-77d1-4566-afd4c50d253e",
6 "quantity": 3
7 },
8 {
9 "productId": "91f7ac8b-2baa-289c-aa50-6d64764f35d3",
10 "quantity": 1,
11 "options": {
12 "choices": {
13 "Weight": "250g",
14 "Ground for": "Filter"
15 }
16 }
17 },
18 {
19 "productId": "5376f9ec-b92e-efa9-e4a1-f4f480aa0d3a",
20 "quantity": 1,
21 "options": {
22 "choices": {
23 "Weight": "500g",
24 "Ground for": "Stovetop"
25 },
26 "customTextFields": [{
27 "title": "Custom package sticker",
28 "value": "Enjoy your coffee!"
29 }]
30 }
31 }
32]
33
34cart.addProducts(products)
35 .then((updatedCart) => {
36 // Products added to cart
37 const cartId = updatedCart._id;
38 const cartLineItems = updatedCart.lineItems;
39 })
40 .catch((error) => {
41 // Products not added to cart
42 console.error(error);
43 });
44
45
46/* Example of returned updatedCart object:
47 *
48 * {
49 * "_id": "5d54aa6f-f653-4bed-8c34-7f1373f88c89",
50 * "status": "INCOMPLETE",
51 * "billingAddress": {
52 * "firstName": "Jane",
53 * "lastName": "Doe",
54 * "email": "",
55 * "phone": "+1234567890",
56 * "company": "undefined",
57 * "vatId": "undefined",
58 * "address": "235 West 23rd Street\nNew York, New York 10011\nUnited States"
59 * },
60 * "appliedCoupon": "null",
61 * "buyerInfo": {
62 * "email": "",
63 * "firstName": "Jane",
64 * "identityType": "ADMIN",
65 * "lastName": "Doe",
66 * "phone": "+1234567890"
67 * },
68 * "buyerNote": "undefined",
69 * "currency": {
70 * "symbol": "$",
71 * "code": "USD"
72 * },
73 * "lineItems": [
74 * {
75 * "id": 16,
76 * "productId": "0873ed58-f88d-77d1-4566-afd4c50d253e",
77 * "name": "Digital product",
78 * "quantity": 3,
79 * "lineItemType": "DIGITAL",
80 * "customTextFields": [],
81 * "options": [],
82 * "price": 20,
83 * "totalPrice": 60,
84 * "mediaItem": "null",
85 * "weight": 0
86 * },
87 * {
88 * "id": 17,
89 * "productId": "91f7ac8b-2baa-289c-aa50-6d64764f35d3",
90 * "name": "Colombian Arabica",
91 * "quantity": 1,
92 * "weight": 0.25,
93 * "sku": "10003",
94 * "lineItemType": "PHYSICAL",
95 * "customTextFields": [],
96 * "mediaItem": {
97 * "src": "wix:image://v1/nsplsh_5033504669385448625573~mv2_d_6000_3376_s_4_2.jpg/file.jpg#originWidth=6000&originHeight=3376",
98 * "type": "IMAGE"
99 * },
100 * "options": [
101 * {
102 * "option": "Weight",
103 * "selection": "250g"
104 * },
105 * {
106 * "option": "Ground for",
107 * "selection": "Filter"
108 * }
109 * ],
110 * "price": 35,
111 * "totalPrice": 35
112 * },
113 * {
114 * "id": 18,
115 * "productId": "5376f9ec-b92e-efa9-e4a1-f4f480aa0d3a",
116 * "name": "Indonesian Blend",
117 * "quantity": 1,
118 * "lineItemType": "PHYSICAL",
119 * "customTextFields": [
120 * {
121 * "title": "Custom package sticker",
122 * "value": "Enjoy your coffee!"
123 * }
124 * ],
125 * "mediaItem": {
126 * "src": "wix:image://v1/nsplsh_316b6449475f3235386255~mv2_d_2977_3951_s_4_2.jpg/file.jpg#originWidth=2977&originHeight=3951",
127 * "type": "IMAGE"
128 * },
129 * "options": [
130 * {
131 * "option": "Weight",
132 * "selection": "500g"
133 * },
134 * {
135 * "option": "Ground for",
136 * "selection": "Stovetop"
137 * }
138 * ],
139 * "price": 35,
140 * "totalPrice": 35,
141 * "weight": 0
142 * }
143 * ],
144 * "shippingInfo": {
145 * "shippingAddress": {
146 * "firstName": "Jane",
147 * "lastName": "Doe",
148 * "email": "",
149 * "phone": "+1234567890",
150 * "company": "undefined",
151 * "vatId": "undefined",
152 * "address": "235 West 23rd Street\nNew York, New York 10011\nUnited States"
153 * }
154 * },
155 * "totals": {
156 * "discount": 0,
157 * "quantity": 5,
158 * "shipping": 0,
159 * "subtotal": 160,
160 * "tax": 0,
161 * "total": 160,
162 * "weight": 0.75
163 * },
164 * "weightUnit": "KG"
165 * }
166 *
167 */