Search.../

onCartChanged( )

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

Description

The onCartChanged() function is a client-side event handler invoked every time the cart changes. It takes a callback function which receives the new Cart object as a parameter.

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.onChange().

To migrate to the new function:

  1. Add the new import statement:

    import { cart } from 'wix-stores'
    javascript | Copy Code
  2. If you plan to migrate all functions that use wix-stores, remove the original import wixStores statement.

  3. Look for any code that uses wixStores.onCartChanged(), and replace it with cart.onChange(). Update your code to work with the new cart.onChange() call and response properties.

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

Syntax

function onCartChanged(handler: CartChangedHandler): void
handler: function CartChangedHandler(cart: CartObj): void

onCartChanged Parameters

NAME
TYPE
DESCRIPTION
handler

The name of the function or the function expression to run when a cart changes.

Returns

This function does not return anything.

Return Type:

void

CartChangedHandler Parameters

NAME
TYPE
DESCRIPTION
cart
CartObj

The changed cart.

Returns

This function does not return anything.

Return Type:

void

Was this helpful?

An event when a cart is changed

Copy Code
1// Updated example using cart.onChange()
2
3/************************************
4 * Global (Site) code (masterPage.js) *
5 ************************************/
6
7import { cart } from 'wix-stores';
8
9$w.onReady(function () {
10 cart.onChange((changedCart) => {
11 const cartId = changedCart._id;
12 const cartLineItems = changedCart.lineItems;
13 });
14});
15
16
17/* Example of returned changedCart object:
18 *
19 * {
20 * "_id": "b36eb035-635a-450e-b74d-acf86ee4dfcc",
21 * "appliedCoupon": {
22 * "couponId": "e81e9c48-f954-4044-ba64-ccfe5c103c8f",
23 * "name": "Summer Sale",
24 * "code": "SummerSale",
25 * "discountValue": "$10.00",
26 * "couponType": "MoneyOff"
27 * },
28 * "billingAddress": {
29 * "firstName": "John",
30 * "lastName": "Doe",
31 * "email":"john.doe@somedomain.com",
32 * "phone":"5555555",
33 * "address":"235 West 23rd Street\nNew York, New York 10011\nUnited States"
34 * },
35 * "buyerNote": "This is a note from the buyer.",
36 * "buyerInfo":{
37 * "firstName": "John",
38 * "lastName": "Doe",
39 * "email": "john.doe@somedomain.com",
40 * "phone": "5555555555",
41 * "identityType": "CONTACT"
42 * },
43 * "status": "INCOMPLETE",
44 * "currency": {
45 * "code": "USD",
46 * "symbol": "$"
47 * },
48 * "shippingInfo": {
49 * "deliveryOption": "Free Shipping",
50 * "shippingAddress": {
51 * "firstName": "John",
52 * "lastName": "Doe",
53 * "email":"john.doe@somedomain.com",
54 * "phone":"5555555",
55 * "address":"235 West 23rd Street\nNew York, New York 10011\nUnited States"
56 * },
57 * "pickupDetails":null
58 * },
59 * "lineItems":[
60 * {
61 * "quantity": 1,
62 * "price": 120,
63 * "name": "A product",
64 * "productId": "a668ef33-f5b8-6569-d04c-1d123be68441",
65 * "totalPrice": 120,
66 * "lineItemType": "PHYSICAL",
67 * "customTextFields": [
68 * "title": "Custom Field",
69 * "value": "Custom value"
70 * ],
71 * "mediaItem": {
72 * "src": "wix:image://v1/a9ff3b_ed3b544c319b4fad9c222c791a997832.jpg/file.jpg#originWidth=1000&originHeight=1000",
73 * "type": "IMAGE"
74 * },
75 * "sku": "21554345656",
76 * "options": [ ],
77 * "weight": 3,
78 * "id": 1
79 * },
80 * {
81 * "quantity": 1,
82 * "price": 25,
83 * "name": "Another product",
84 * "productId": "1a2d7e83-4bef-31d5-09e1-3326ee271c09",
85 * "totalPrice": 25,
86 * "lineItemType": "PHYSICAL",
87 * "mediaItem": {
88 * "src": "wix:image://v1/a9ff3b_c6158b4d41784ae8b08337a331e1de7f.jpg/file.jpg#originWidth=1000&originHeight=1000",
89 * "type": "IMAGE"
90 * },
91 * "sku": "217537123517253",
92 * "options": [
93 * {
94 * "option": "Size",
95 * "selection": "Medium"
96 * },
97 * {
98 * "option": "Color",
99 * "selection": "Black"
100 * }
101 * ],
102 * "weight": 2,
103 * "id": 2
104 * }
105 * ],
106 * "totals": {
107 * "discount": 0,
108 * "quantity": 2,
109 * "shipping": 0,
110 * "subtotal": 145,
111 * "tax": 0,
112 * "total": 145,
113 * "weight": 5
114 * },
115 * "weightUnit": "LB"
116 * }
117 *
118 */
119
120// Deprecated example
121
122/************************************
123 * Global (Site) code (masterPage.js) *
124 ************************************/
125
126import wixStores from 'wix-stores';
127
128$w.onReady(function () {
129 wixStores.onCartChanged((cart) => {
130 const cartId = cart._id;
131 const cartLineItems = cart.lineItems;
132 });
133});