Search...
addToCart( )
Adds catalog line items to a cart.
Description
The addToCart()
function returns a Promise that resolves to the updated cart when the specified items have been added.
Note: When adding catalog items,
options.lineItems.catalogReference
is required.
Syntax
function addToCart(_id: string, options: AddToCartOptions): Promise<AddToCartResponse>
addToCart Parameters
NAME
TYPE
DESCRIPTION
_id
string
Cart ID.
options
Optional
AddToCartOptions
Items to be added to cart.
Returns
Fulfilled - Cart.
Return Type:
Promise<
AddToCartResponse
>NAME
TYPE
DESCRIPTION
cart
Cart
Updated cart.
Was this helpful?
Add a catalog item to a cart
Copy Code
1/**************************************2 * Backend code - my-backend-file.web.js *3 *************************************/45import { Permissions, webMethod } from 'wix-web-module';6import { cart } from 'wix-ecom-backend';78export const myAddToCartFunction = webMethod(Permissions.Anyone, async (_id, options) => {9 try {10 const updatedCart = await cart.addToCart(_id, options);11 console.log('Success! Updated cart:', updatedCart);12 return updatedCart;13 } catch (error) {14 console.error(error);15 // Handle the error16 }17});1819/*************20 * Page code *21 ************/2223import { myAddToCartFunction } from 'backend/my-backend-file.web';2425// Sample addToCart function parameters:26const cartId = '96a61a4b-6b61-47d1-a039-0213a8230ccd';27const options = {28 "lineItems": [{29 "catalogReference": {30 // Wix Stores appId31 "appId": "215238eb-22a5-4c36-9e7b-e7c08025e04e",32 // Wix Stores productId33 "catalogItemId": "c8539b66-7a44-fe18-affc-afec4be8562a"34 },35 "quantity": 136 }]37};3839myAddToCartFunction(cartId, options)40 .then((updatedCart) => {41 const cartSubtotal = updatedCart.subtotal.amount;42 const cartCheckoutId = updatedCart.checkoutId;43 const numberOfCartItems = updatedCart.lineItems.length;4445 console.log('Success! Updated cart:', updatedCart);46 return updatedCart;47 })48 .catch((error) => {49 console.error(error);50 // Handle the error51 });5253/* Promise resolves to:54 *55 * {56 * "_id": "ba47a627-7bb8-4918-89b2-6a72af464765",57 * "lineItems": [58 * {59 * "_id": "00000000-0000-0000-0000-000000000001",60 * "quantity": 1,61 * "catalogReference": {62 * "catalogItemId": "c8539b66-7a44-fe18-affc-afec4be8562a",63 * "appId": "215238eb-22a5-4c36-9e7b-e7c08025e04e"64 * },65 * "productName": {66 * "original": "Shirt",67 * "translated": "Shirt"68 * },69 * "url": "https://example.wixsite.com",70 * "price": {71 * "amount": "10",72 * "convertedAmount": "10",73 * "formattedAmount": "€10.00",74 * "formattedConvertedAmount": "€10.00"75 * },76 * "fullPrice": {77 * "amount": "10",78 * "convertedAmount": "10",79 * "formattedAmount": "€10.00",80 * "formattedConvertedAmount": "€10.00"81 * },82 * "priceBeforeDiscounts": {83 * "amount": "10",84 * "convertedAmount": "10",85 * "formattedAmount": "€10.00",86 * "formattedConvertedAmount": "€10.00"87 * },88 * "descriptionLines": [],89 * "image": "wix:image://v1/3c76e2_c5331f937348492a97df87b0a3b34ea4~mv2.jpg#originWidth=1000&originHeight=1000",90 * "availability": {91 * "status": "AVAILABLE"92 * },93 * "physicalProperties": {94 * "sku": "364115376135191",95 * "shippable": true96 * },97 * "couponScopes": [98 * {99 * "namespace": "stores",100 * "group": {101 * "name": "collection",102 * "entityId": "00000000-000000-000000-000000000001"103 * }104 * },105 * {106 * "namespace": "stores",107 * "group": {108 * "name": "product",109 * "entityId": "c8539b66-7a44-fe18-affc-afec4be8562a"110 * }111 * }112 * ],113 * "itemType": {114 * "preset": "PHYSICAL"115 * },116 * "paymentOption": "FULL_PAYMENT_ONLINE"117 * }118 * ],119 * "buyerInfo": {120 * "visitorId": "4c7ce95c-9fb3-417d-9f02-b41e82b841f7"121 * },122 * "currency": "EUR",123 * "conversionCurrency": "EUR",124 * "buyerLanguage": "en",125 * "siteLanguage": "en",126 * "taxIncludedInPrices": false,127 * "weightUnit": "KG",128 * "subtotal": {129 * "amount": "10",130 * "convertedAmount": "10",131 * "formattedAmount": "€10.00",132 * "formattedConvertedAmount": "€10.00"133 * },134 * "appliedDiscounts": [],135 * "inSync": true,136 * "_createdDate": "2022-05-15T11:31:30.484Z",137 * "_updatedDate": "2022-05-23T12:11:55.095Z"138 * }139 *140 */