Docs › Luv Cart

Developer API & events

Luv Cart works on most themes with no code. When you want your own buttons, apps or analytics to work with the drawer, everything on this page is public and safe to build on.

Open the drawer from anything

Add data-luvcart-open to any element and a click on it opens the drawer:

<button type="button" data-luvcart-open>View cart</button>

While the drawer is open, the page's <html> element carries data-luvcart-open="true", so other scripts, such as a popup or a chat bubble, can wait, and your CSS can react:

html[data-luvcart-open="true"] .chat-bubble { display: none; }

window.LuvCart

Available once the drawer has loaded (listen for luvcart:ready):

function whenLuvCart(fn) {
  if (window.LuvCart) fn();
  else document.addEventListener("luvcart:ready", fn, { once: true });
}
whenLuvCart(async function () {
  await LuvCart.addItem(44012345678, 1, { _source: "quiz" });
  LuvCart.open();
});

Events

Luv Cart dispatches these on document. Each event's detail includes the cart as detail.cart, plus what the event is about.

document.addEventListener("luvcart:added", function (e) {
  console.log("Items in cart:", e.detail.cart.item_count);
});

Luv Cart listens, too. Dispatch luvcart:refresh on document after your code changes the cart and the drawer redraws. It also answers the common theme events cart:refresh, cart:updated and cart-drawer:open.

Hooks

Define window.LuvCartHooks to adjust what the drawer does:

window.LuvCartHooks = {
  // Return false to skip an add (items: the lines about to be added)
  beforeAdd: function (items) { return true; },
  // Return false to stop checkout, for example until terms are accepted
  beforeCheckout: function (cart) {
    var box = document.querySelector("#terms");
    return !box || box.checked;
  },
  // Reorder, trim or extend the upsell list, then return it
  upsells: function (list) { return list.slice(0, 2); }
};

Settings for unusual themes

Under Behavior in the app you can list CSS selectors for your theme's own cart drawer to hide, for extra buttons that should open Luv Cart, and for add-to-cart buttons Luv Cart should handle. Most themes need none of these.

Luv Cart's marks on lines and orders

These properties start with an underscore, so shoppers don't see them in the drawer or at checkout. You'll find them on the order in your Shopify admin.

Don't add these yourself: a gift line is only free when Luv Cart added it for a gift the cart earned.

Questions? hi@customerluv.app — a founder answers every message.

Your first two weeks free ×