Configuration parameter

mutations

A configuration parameter which lets you define cart mutation functions.

Define mutations

Use the conf method of the liquidAjaxCart object to define functions:

function myMutation1 () {
  // ... mutation code ...
}

function myMutation2 () {
  // ... mutation code ...
}

window.liquidAjaxCart.conf('mutations', [
  myMutation1,
  myMutation2
]);

Liquid Ajax Cart will call these function when a page is just loaded and each time after a user changes their cart.

Returning object structure

Each function is expected to return either nothing (if nothing should be changed in the cart) or an object with the information on what Shopify Cart API Ajax requests should be performed:

function myMutation () {
  // ... mutation code ...

  return {
    requests: [
      {
        type: "change", // remove the first item from the cart
        body: {
          line: 1,
          quantity: 0
        }
      },
      {
        type: "add", // add the product with "40934235668668" variant id 
        body: {
          items: [{
            id: 40934235668668,
            quantity: 1,
          }]
        }
      }
    ]
  };
}

requests

An array of objects each of which contains information on one Shopify Cart API Ajax request that should be performed.

type

Each requests array’s object must have the type property. The available types are add, get, update, change, clear, according to Shopify Cart API endpoints.

body

The body object will be passed to the Shopify Cart API endpoint as is, so read what data Shopify expects in the Shopify Cart API endpoints documentation.

Examples

Find detailed explanation with examples of how to create mutation functions in the “Auto-add/remove cart items” guide.