State

State is a Javascript object where Liquid Ajax Cart keeps the information related to user’s cart.

{
  "cart":{
    "token":"b7d3743e2c398043f209c5a3a9014f9d",
    "note":null,
    "attributes":{},
    "original_total_price":1000,
    "total_price":1000,
    "total_discount":0,
    "total_weight":0,
    "item_count":1,
    "items":[{}],
    "requires_shipping":false,
    "currency":"USD",
    "items_subtotal_price":1000,
    "cart_level_discount_applications":[]
  },
  "previousCart":{},
  "status":{
    "requestInProgress":false,
    "cartStateSet":true
  }
}

State gets updated after each Shopify Cart API request.

Always use the functions from the Cart Ajax API requests reference instead of direct Cart API calls and Liquid Ajax Cart will make sure that the State is up to date.

Properties

cart

Contains JSON versions of the Shopify cart liquid object.

The property data is getting loaded from the data-ajax-cart-initial-state script.

The property gets updated after each successful Shopify Cart API request.

previousCart

Contains a JSON object that was in the cart property before the cart was updated due to the last successful Shopify Cart API request.

The cart and the previousCart properties are not always different. If the previous Shopify Cart API request didn’t make any changes in the cart but returned Shopify cart JSON-data, for example a “Get” request or an empty “Update” request, the cart and the previousCart will be 100% same.

status.cartStateSet

The status.cartStateSet is true if the cart property is loaded.

The js-ajax-cart-set CSS class will be added to the body tag if the property is true.

status.requestInProgress

The status.requestInProgress is true if there are one or more Shopify Cart API requests in Queues.

If the property is true:

Interaction

Use the getCartState function to get the current state.

If you want to run your Javascript code each time when the state is get updated — use the subscribeToCartStateUpdate function.


Reference