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:
Add the new import statement:
javascript | Copy Codeimport { cart } from 'wix-stores'Look for any code that uses
$w('#shoppingCartIcon').addToCart()
, and replace it with withcart.addProducts()
.Note:
'#shoppingCartIcon'
might be named differently in your code.Update your code to work with the new
cart.addProducts()
call and response properties.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
The ID of the product to add to the cart.
The number of product units to add to the cart. If omitted, one product unit will be added.
Product options.
Returns
Fulfilled - When the product has been added to the cart.
Return Type:
Was this helpful?
1// Updated example using cart.addProducts()23import { cart } from 'wix-stores';45const products = [{6 "productId": "62a3782f-f1b2-411e-b2a3-9c143eb7fc84",7 "quantity": 18}]910cart.addProducts(products)11 .then((updatedCart) => {12 // Products added to cart13 const cartId = updatedCart._id;14 const cartLineItems = updatedCart.lineItems;15 })16 .catch((error) => {17 console.log(error);18 });192021// Deprecated example2223$w('#myShoppingCartIcon').addToCart("62a3782f-f1b2-411e-b2a3-9c143eb7fc84")24 .then(() => {25 console.log("Product added");26 })27 .catch((error) => {28 console.log(error);29 });
1// Updated example using cart.addProducts()23import { cart } from 'wix-stores';45$w("#myButton").onClick(() => {6 cart.addProducts([{7 "productId": "62a3782f-f1b2-411e-b2a3-9c143eb7fc84",8 "quantity": 19 }])10 .then((updatedCart) => {11 // Products added to cart12 const cartId = updatedCart._id;13 const cartLineItems = updatedCart.lineItems;14 })15 .catch((error) => {16 console.log(error);17 });18});192021// Deprecated example2223$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});
1// Updated example using cart.addProducts()23import { cart } from 'wix-stores';45cart.addProducts([{6 "productId": "62a3782f-f1b2-411e-b2a3-9c143eb7fc84",7 "quantity": 28}])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 });171819// Deprecated example2021$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 });
1// Updated example using cart.addProducts()23import { cart } from 'wix-stores';45const 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]2425cart.addProducts(products)26 .then((updatedCart) => {27 // Products added to cart28 const cartId = updatedCart._id;29 const cartLineItems = updatedCart.lineItems;30 })31 .catch((error) => {32 console.log(error);33 });343536// Deprecated example3738$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 });