Get the number of items in the cart

When you implement the Firmhouse Storefront SDK on a store, you might want to show a count of the number of products in the cart.

To fetch the number of items in the cart, use the following async method.

Firmhouse.currentCart().then((cart) => {
  const numberOfProducts = cart.orderedProducts.length;
  const totalQuantity = cart.orderedProducts.reduce((sum, orderedProduct) => sum + orderedProduct.quantity, 0);

  console.log({ numberOfProducts, totalQuantity });
})

Or wrap it in an eventListeren for continuous updates:

window.addEventListener("firmhouse:cart_updated", (e) => {
  const numberOfProducts = e.detail.orderedProducts.length;
  const totalQuantity = e.detail.orderedProducts.reduce((sum, orderedProduct) => sum + orderedProduct.quantity, 0);

  console.log({ numberOfProducts, totalQuantity });
});

Replace the console.log with your own way to display the data :wink: