JavaScript API

The liquidAjaxCart object

An object which exposes the methods and properties of the Liquid Ajax Cart library. The liquidAjaxCart object is defined as a property of the window object to be accessible from everywhere.

Methods and properties

A few of them are available as soon as the library code is loaded:

  • conf() — sets configuration parameter values;
  • init — initialization status (boolean).

The rest of them are available only after the initialization:

  • get() — makes a Shopify Cart API GET /cart.js Ajax request;
  • add() — makes a POST /cart/add.js request;
  • change() — makes a POST /cart/change.js request;
  • update() — makes a POST /cart/update.js request;
  • clear() — makes a POST /cart/clear.js request;
  • cart — current cart state (JSON);
  • processingtrue if there is a request in progress now (boolean).

Access after initialization

Since most of the liquidAjaxCart methods and properties are available only after the initialization, use the liquid-ajax-cart:init event along with the liquidAjaxCart.init property when your code depends on Liquid Ajax Cart and should be executed as soon as the library is fully available:

assets/script.js
function runWhenInit() {
  console.log("The current cart state is: ", window.liquidAjaxCart.cart);
}

if (window.liquidAjaxCart?.init) {
  // if Liquid Ajax Cart is already initialized
  runWhenInit();
} else {
  // wait for Liquid Ajax Cart to be initialized
  document.addEventListener("liquid-ajax-cart:init", runWhenInit);
}