Search.../

addToCart( )

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

Description

The addToCart() function returns a Promise that is resolved when the specified product is 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').addToCart(), 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 addToCart(productID: string, [quantity: number], [options: AddToCartOptions]): Promise<void>

addToCart Parameters

NAME
TYPE
DESCRIPTION
productID
string

The ID of the product to add to the cart.

quantity
Optional
number

The number of product units to add to the cart. If omitted, one product unit will be added.

options
Optional
AddToCartOptions

Product options.

Returns

Fulfilled - When the product has been added to the cart.

Return Type:

Promise<void>

Was this helpful?

Add a product to the cart

Copy Code
1// Updated example using cart.addProducts()
2
3import { cart } from 'wix-stores';
4
5const products = [{
6 "productId": "62a3782f-f1b2-411e-b2a3-9c143eb7fc84",
7 "quantity": 1
8}]
9
10cart.addProducts(products)
11 .then((updatedCart) => {
12 // Products added to cart
13 const cartId = updatedCart._id;
14 const cartLineItems = updatedCart.lineItems;
15 })
16 .catch((error) => {
17 console.log(error);
18 });
19
20
21// Deprecated example
22
23$w('#myShoppingCartIcon').addToCart("62a3782f-f1b2-411e-b2a3-9c143eb7fc84")
24 .then(() => {
25 console.log("Product added");
26 })
27 .catch((error) => {
28 console.log(error);
29 });
Add a product to the cart from a button click

Copy Code
1// Updated example using cart.addProducts()
2
3import { cart } from 'wix-stores';
4
5$w("#myButton").onClick(() => {
6 cart.addProducts([{
7 "productId": "62a3782f-f1b2-411e-b2a3-9c143eb7fc84",
8 "quantity": 1
9 }])
10 .then((updatedCart) => {
11 // Products added to cart
12 const cartId = updatedCart._id;
13 const cartLineItems = updatedCart.lineItems;
14 })
15 .catch((error) => {
16 console.log(error);
17 });
18});
19
20
21// Deprecated example
22
23$w("#myButton").onClick(() => {
24 $w("#myShoppingCartIcon").addToCart("62a3782f-f1b2-411e-b2a3-9c143eb7fc84")
25 .then(() => {
26 console.log("Product added");
27 })
28 .catch((error) => {
29 console.log(error);
30 });
31});
Add two units of a product to the cart

Copy Code
1// Updated example using cart.addProducts()
2
3import { cart } from 'wix-stores';
4
5cart.addProducts([{
6 "productId": "62a3782f-f1b2-411e-b2a3-9c143eb7fc84",
7 "quantity": 2
8}])
9 .then((updatedCart) => {
10 console.log("Product added");
11 const cartId = updatedCart._id;
12 const cartLineItems = updatedCart.lineItems;
13 })
14 .catch((error) => {
15 console.log(error);
16 });
17
18
19// Deprecated example
20
21$w('#myShoppingCartIcon').addToCart("62a3782f-f1b2-411e-b2a3-9c143eb7fc84", 2)
22 .then(() => {
23 console.log("Product added");
24 })
25 .catch((error) => {
26 console.log(error);
27 });
Add a product to the cart with options

Copy Code
1// Updated example using cart.addProducts()
2
3import { cart } from 'wix-stores';
4
5const products = [
6 {
7 "productId": "62a3782f-f1b2-411e-b2a3-9c143eb7fc84",
8 "quantity": 2,
9 "options": {
10 "choices": {
11 "Size": "Small"
12 },
13 "customTextFields": [{
14 "title": "Personalization 1",
15 "value": "Personalized Text 1"
16 },
17 {
18 "title": "Personalization 2",
19 "value": "Personalized Text 2"
20 }]
21 }
22 }
23]
24
25cart.addProducts(products)
26 .then((updatedCart) => {
27 // Products added to cart
28 const cartId = updatedCart._id;
29 const cartLineItems = updatedCart.lineItems;
30 })
31 .catch((error) => {
32 console.log(error);
33 });
34
35
36// Deprecated example
37
38$w('#shoppingCartIcon1').addToCart("62a3782f-f1b2-411e-b2a3-9c143eb7fc84", 2, {
39 "choices": {
40 "Size": "Small"
41 },
42 "customTextFields": [{
43 "title": "Personalization 1",
44 "value": "Personalized Text 1"
45 },
46 {
47 "title": "Personalization 2",
48 "value": "Personalized Text 2"
49 }]
50})
51 .then(() => {
52 console.log("Product added");
53 })
54 .catch((error) => {
55 console.log(error);
56 });