Cart Updated

An event that keeps an eye on cart of the app.

Description

The cartUpdated event triggers whenever the contents of the cart change —
such as when a product is added, removed, or its quantity changes. It provides the updated cart object so you can refresh UI components like cart totals, icons, or badges.

Function Signature

VajroSDK.listeners.onCartUpdated(() => {
  // Write your logic
});
ParameterTypeRequiredDescription
NilNilNilThis listener does not require any parameter.

Example Usage

let cartTotal = VajroSDK.variables.cart.subtotal || 0;
VajroSDK.listeners.onCartUpdated(() => {
    cartTotal = VajroSDK.variables.cart.subtotal || 0;
    if (cartTotal > 500) {
        VajroSDK.actions.showToast({
            title: "Congrats",
            message: "You are eligible for Free Shipping!!",
        })
    }
});

Best Practices

  1. Debounce updates: Avoid flickering by batching UI updates if multiple events fire quickly.
  2. Always read totals from payload: Avoid caching cart totals to prevent outdated values.

Caveats

  1. Global event: Fires on any page where the cart changes.
  2. Frequency: May fire multiple times for a single cart action (e.g., quantity change).