Total weight of cart

Does anyone have an idea or know how I can get the total weight of customers cart?

Hey @darkotstecktnedri,

You can retrieve current cart items using our store that is exposed via

const state = Snipcart.store.getState();

Here’s an example on how you can retrieve to total weight:

const state = Snipcart.store.getState();

var totalWeight = state.cart.items
  .items.reduce((acc, item) => { 
    return acc + item.quantity * item.dimensions.weight;
  }, 0);

console.log(totalWeight);

You can also watch to state changes to make sure you always have the latest value:

document.addEventListener("snipcart.ready", () => {
  let cartTotalWeight = 0;

  Snipcart.store.subscribe(() => {
    const state = Snipcart.store.getState();

    var weight = state.cart.items.items.reduce((acc, item) => {
      return acc + item.quantity * item.dimensions.weight;
    }, 0);

    if (weight != cartTotalWeight) {
      cartTotalWeight = weight;
      console.log(cartTotalWeight);
    }
  });
});
1 Like

That’s exactly what we were searching for. Thanks a lot!