Search.../

addProductsToCart( )

Deprecated. This function will continue to work, but a newer version is available at wix-stores.cart.addProducts().

Description

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

Migration Instructions

If this function is already in your code, it will continue to work. To stay compatible with future changes, migrate to wix-stores.cart.addProducts().

To migrate to the new function:

  1. Add the new import statement:

    import { cart } from 'wix-stores'
    javascript | Copy Code
  2. Look for any code that uses $w('#shoppingCartIcon').addProductsToCart(), and replace it with with cart.addProducts().

    Note: '#shoppingCartIcon' might be named differently in your code.

  3. Update your code to work with the new cart.addProducts() call and response properties.

  4. Test your changes to make sure your code behaves as expected.

Syntax

function addProductsToCart(products: Array<AddToCartItem>): Promise<void>

addProductsToCart Parameters

NAME
TYPE
DESCRIPTION
products
Array<AddToCartItem>

The list of products to add to the cart.

Returns

Fulfilled - When the products have been added to the cart.

Return Type:

Promise<void>

Was this helpful?

Add multiple products to the cart

Copy Code
1// Updated example using cart.addProducts()
2
3import { cart } from 'wix-stores';
4
5const products = [
6 {
7 "productId": "580a3cd8-8e39-2863-2a56-1d7019cbfcc1",
8 "quantity": 5
9 },
10 {
11 "productId": "2ab0eac2-69b3-bc77-5033-fac8805b223a",
12 "quantity": 2,
13 "options": {
14 "choices": {
15 "Size": "Small"
16 },
17 "customTextFields": [{
18 "title": "Personalization 1",
19 "value": "Personalized Text 1"
20 },
21 {
22 "title": "Personalization 2",
23 "value": "Personalized Text 2"
24 }]
25 }
26 }
27]
28
29cart.addProducts(products)
30 .then((updatedCart) => {
31 console.log("Product added");
32 const cartId = updatedCart._id;
33 const cartLineItems = updatedCart.lineItems;
34 })
35 .catch((error) => {
36 console.log(error);
37 });
38
39
40
41// Deprecated example
42
43
44$w('#shoppingCartIcon1').addProductsToCart([
45 {
46 "productId": "580a3cd8-8e39-2863-2a56-1d7019cbfcc1",
47 "quantity": 5
48 },
49 {
50 "productId": "2ab0eac2-69b3-bc77-5033-fac8805b223a",
51 "quantity": 2,
52 "options": {
53 "choices": {
54 "Size": "Small"
55 },
56 "customTextFields": [{
57 "title": "Personalization 1",
58 "value": "Personalized Text 1"
59 },
60 {
61 "title": "Personalization 2",
62 "value": "Personalized Text 2"
63 }]
64 }
65 }
66])
67 .then(() => {
68 console.log("Products added");
69 })
70 .catch((error) => {
71 console.log(error);
72 });