Liquid Ajax Cart provides methods for executing Shopify Cart API Ajax requests, as well as properties and events to extend functionality.
Once Liquid Ajax Cart is loaded, it does the following:
liquidAjaxCart objectThe liquidAjaxCart object is defined
as a property of the window object to be accessible from everywhere.
Once created, it has:
init property — reflects the initialization status of the library, which is false at the moment.conf() method — lets you set configuration parameters.<script type="module">
import '{{ "liquid-ajax-cart-v2.2.0.js" | asset_url }}';
// Is Liquid Ajax Cart initialized: false
console.log("Is Liquid Ajax Cart initialized: ", window.liquidAjaxCart.init);
// Configuration function is available right after import
window.liquidAjaxCart.conf('updateOnWindowFocus', false);
</script>
Firstly, Liquid Ajax Cart attempts to retrieve the cart state data from the first found
script element with the data-ajax-cart-initial-state attribute,
which is supposed to contain the cart data in the JSON format, as shown in the “Installation” guide.
If there is no such element found, Liquid Ajax Cart performs a Shopify Cart API Ajax request to retrieve the cart state data.
liquidAjaxCart objectWhen the cart state data is received, the init property becomes true.
Also, Liquid Ajax Cart populates the liquidAjaxCart object
with the following methods and properties:
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 — keeps the current cart state data;processing — true if there is a request in progress now.js-ajax-cart-init CSS classThe js-ajax-cart-init CSS class is added to the html tag.
liquid-ajax-cart:init eventSince 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:
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);
}
Liquid Ajax Cart provides the methods
get,
add,
change,
update,
clear
of the liquidAjaxCart object to perform Shopify Cart API Ajax requests.
Use these methods rather than fetch or any other way of sending requests.
Otherwise, Liquid Ajax Cart won’t update the cart-related content on the page.
Liquid Ajax Cart uses these methods as well when performing Shopify Cart API Ajax requests.
For example, when a user clicks an “Add to cart” button in a product form,
wrapped with the <ajax-cart-product-form> custom tag,
Liquid Ajax Cart calls the liquidAjaxCart.add() method
in order to performs a Shopify Cart API /cart/add.js Ajax request.
When one of the methods
get,
add,
change,
update,
clear
of the liquidAjaxCart object is called,
Liquid Ajax Cart adds a Shopify Cart API Ajax request to the Queue of requests.
Once the Queue isn’t empty, Liquid Ajax Cart switches to the “processing requests” mode and executes requests according to their order until the Queue is empty.
When Liquid Ajax Cart is in the “processing requests” mode,
the liquidAjaxCart.processing property is true
and the js-ajax-cart-processing CSS class is added to the html tag so that you can show a loading indicator.
When one of the methods
get,
add,
change,
update,
clear
of the liquidAjaxCart object is called,
Liquid Ajax Cart adds a Shopify Cart API Ajax request to the Queue of requests.
If the Queue isn’t empty when a new request is getting added, that means Liquid Ajax Cart is already in the “processing requests” mode, and the new request will be added to the end of the Queue and executed when the previous requests are finished:
#1: [/cart/change.js — executing]
#2: [/cart/add.js — new added request]
Each request is executed according to the Request lifecycle.
If the Queue is empty when a new request is getting added, that means Liquid Ajax Cart is in the “idle” mode and has to switch to the “request processing” mode. Liquid Ajax Cart does the following:
As the Queue has been empty, the new request will be the first:
#1: [/cart/add.js — new added request]
liquidAjaxCart.processing to trueSince Liquid Ajax Cart was in the “idle” mode,
the liquidAjaxCart.processing property was false.
Now the Queue of requests has a request to execute,
and the liquidAjaxCart.processing property becomes true.
js-ajax-cart-processing CSS classThe js-ajax-cart-processing CSS class is added to the html tag;
liquid-ajax-cart:queue-start eventAs Liquid Ajax Cart is about to start executing requests from the Queue,
the liquid-ajax-cart:queue-start event is fired.
Liquid Ajax Cart executes the first request in the Queue:
#1: [/cart/add.js — executing]
If one of the methods
get,
add,
change,
update,
clear
of the liquidAjaxCart object is called while the request is executing,
Liquid Ajax Cart will add the new requests to the end of the Queue and
execute them until the Queue is empty:
#1: [/cart/add.js — executing]
#2: [/cart/change.js]
Each request is executed according to the Request lifecycle.
liquidAjaxCart.processing to falseOnce all requests are executed and the Queue is empty,
Liquid Ajax Cart is switching back to the “idle” mode,
thus it sets liquidAjaxCart.processing property was false.
js-ajax-cart-processing CSS classThe js-ajax-cart-processing CSS class is removed from the html tag;
liquid-ajax-cart:queue-end eventAs Liquid Ajax Cart has executed all the requests from the Queue
and it is switching back to the “idle” mode,
the liquid-ajax-cart:queue-end event is fired.
Liquid Ajax Cart does the folowing when performs a Shopify Cart API Ajax request:
Liquid Ajax Cart creates a Request state object for each Shopify Cart API request. When created, the object has the following properties:
{
"endpoint": "/cart/add.js",
"requestBody": {…},
"requestType": "add",
"info": {…}
}
Liquid Ajax Cart adds the sections parameter to the request body
with the IDs of the Shopify sections that contain data-ajax-cart-section elements.
Shopify will return the re-rendered HTML of the sections in response to the request.
(Bundled section rendering).
The modified requestBody property of the Request state object:
{
"endpoint": "/cart/add.js",
"requestBody": {
"items": [
{
"id": 40934235668668,
"quantity": 1
}
],
"sections": "my-ajax-cart"
},
"requestType": "add",
"info": {…}
}
data-ajax-cart-errors elements.Liquid Ajax Cart empties the data-ajax-cart-errors elements related to the request.
For example, if the request was initiated by a product form, only the data-ajax-cart-errors element
that belongs to the form, will be emptied.
liquid-ajax-cart:request-start eventThe liquid-ajax-cart:request-start event is fired a moment before the request is performed.
You can read, modify or cancel the request in your event listener.
Liquid Ajax Cart performs the request if it isn’t cancelled
(the info.cancel property of the Request state object isn’t true).
Otherwise, this step is skipped.
If the request is successful but the response doesn’t have the cart state object (the /cart/add.js endpoint is the case),
or if the response doesn’t have HTML of all Shopify sections needed,
Liquid Ajax Cart performs an additional /cart/update.js request to get the data.
Liquid Ajax Cart populates the Request state object according to the request result
— the responseData / extraResponseData / fetchError properties:
{
"endpoint": "/cart/add.js",
"requestBody": {…},
"requestType": "add",
"info": {…},
"responseData": {
"ok": true,
"status": 200,
"body": {…}
},
"extraResponseData": {…}
}
firstCallback functiondata-ajax-cart-section elementsIf the request is successful, Liquid Ajax Cart retrieves the re-rendered HTML
of the data-ajax-cart-section elements from the response.
It then replaces the current data-ajax-cart-section elements’ outer HTML
with the new one.
data-ajax-cart-errors elementsIf the request is failed, Liquid Ajax Cart inserts the error message to the related data-ajax-cart-errors element.
liquidAjaxCart.cart propertyIf the request is successful, Liquid Ajax Cart extracts the cart state data from the response
and stores it within the liquidAjaxCart.cart property.
The data-ajax-cart-bind elements HTML,
the data-ajax-cart-quantity-input
and data-ajax-cart-property-input input values,
as well as the js-ajax-cart-empty and js-ajax-cart-not-empty CSS classes,
get updated according to the cart state data.
liquid-ajax-cart:request-end eventThe liquid-ajax-cart:request-end is the only event that is fired right after a request is executed,
the cart state (liquidAjaxCart.cart) is updated
and the cart-related content is re-rendered.
lastCallback function