Adds catalog line items to the current site visitor's cart.
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.
function addToCurrentCart(
options: AddToCurrentCartOptions,
): Promise<AddToCartResponse>;
Items to be added to the current cart.
/*****************************************
* Backend code - my-backend-file.web.js *
****************************************/
import { Permissions, webMethod } from "wix-web-module";
import { currentCart } from "wix-ecom-backend";
export const myAddToCurrentCartFunction = webMethod(
Permissions.Anyone,
async (options) => {
try {
const updatedCurrentCart = await currentCart.addToCurrentCart(options);
console.log("Success! Updated current cart:", updatedCurrentCart);
return updatedCurrentCart;
} catch (error) {
console.error(error);
// Handle the error
}
},
);
/*************
* Page code *
************/
import { myAddToCurrentCartFunction } from "backend/my-backend-file.web";
// Sample options object:
const options = {
lineItems: [
{
catalogReference: {
// Wix Stores appId
appId: "215238eb-22a5-4c36-9e7b-e7c08025e04e",
// Wix Stores productId
catalogItemId: "1a2d7e83-4bef-31d5-09e1-3326ee271c09",
options: {
// Wix Stores variantId
variantId: "132b84e8-aab8-47a1-a1f6-2c47557b64a4",
},
},
quantity: 1,
},
],
};
myAddToCurrentCartFunction(options)
.then((updatedCurrentCart) => {
const cartId = updatedCurrentCart._id;
const numberOfCartLineItems = updatedCurrentCart.lineItems.length;
console.log("Success! Updated cart:", updatedCurrentCart);
return updatedCurrentCart;
})
.catch((error) => {
console.error(error);
// Handle the error
});
/* Promise resolves to:
*
* {
* "_id": "e4156539-32b8-48dd-97f8-164b5a5b8740",
* "lineItems": [
* {
* "_id": "00000000-0000-0000-0000-000000000001",
* "quantity": 1,
* "catalogReference": {
* "catalogItemId": "1a2d7e83-4bef-31d5-09e1-3326ee271c09",
* "appId": "215238eb-22a5-4c36-9e7b-e7c08025e04e",
* "options": {
* "variantId": "132b84e8-aab8-47a1-a1f6-2c47557b64a4"
* }
* },
* "productName": {
* "original": "Watch",
* "translated": "Watch"
* },
* "url": "https://example.wixsite.com",
* "price": {
* "amount": "30",
* "convertedAmount": "30",
* "formattedAmount": "€30.00",
* "formattedConvertedAmount": "€30.00"
* },
* "fullPrice": {
* "amount": "30",
* "convertedAmount": "30",
* "formattedAmount": "€30.00",
* "formattedConvertedAmount": "€30.00"
* },
* "priceBeforeDiscounts": {
* "amount": "30",
* "convertedAmount": "30",
* "formattedAmount": "€30.00",
* "formattedConvertedAmount": "€30.00"
* },
* "descriptionLines": [
* {
* "name": {
* "original": "Size",
* "translated": "Size"
* },
* "plainText": {
* "original": "Medium",
* "translated": "Medium"
* },
* "lineType": "UNRECOGNISED"
* },
* {
* "name": {
* "original": "Color",
* "translated": "Color"
* },
* "colorInfo": {
* "original": "Grey",
* "translated": "Grey",
* "code": "rgb(128, 128, 128)"
* },
* "lineType": "UNRECOGNISED"
* }
* ],
* "image": "wix:image://v1/3c76e2_8891bbe3372a428aac976ac59aa0ac74~mv2.jpg#originWidth=1000&originHeight=1000",
* "availability": {
* "status": "AVAILABLE"
* },
* "physicalProperties": {
* "sku": "217537123517253",
* "shippable": true
* },
* "couponScopes": [
* {
* "namespace": "stores",
* "group": {
* "name": "collection",
* "entityId": "00000000-000000-000000-000000000001"
* }
* },
* {
* "namespace": "stores",
* "group": {
* "name": "product",
* "entityId": "1a2d7e83-4bef-31d5-09e1-3326ee271c09"
* }
* }
* ],
* "itemType": {
* "preset": "PHYSICAL"
* },
* "paymentOption": "FULL_PAYMENT_ONLINE"
* }
* ],
* "buyerInfo": {
* "memberId": "c43190d2-eea3-493e-b6e8-f146850c6873"
* },
* "currency": "EUR",
* "conversionCurrency": "EUR",
* "buyerLanguage": "en",
* "siteLanguage": "en",
* "taxIncludedInPrices": false,
* "weightUnit": "KG",
* "subtotal": {
* "amount": "30",
* "convertedAmount": "30",
* "formattedAmount": "€30.00",
* "formattedConvertedAmount": "€30.00"
* },
* "appliedDiscounts": [],
* "inSync": false,
* "_createdDate": "2022-05-23T13:17:46.801Z",
* "_updatedDate": "2022-05-23T13:17:46.801Z"
* }
*
*/
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.