add new handlebars helper cartTotalItems and pass session.cart to it from layouts/layout.hbs to show cart total items including quantities

master
Marty Sloan 2018-06-21 18:21:14 -04:00 committed by Mark Moffat
parent 4f7eeb87c8
commit 9662d341c8
2 changed files with 7 additions and 1 deletions

6
app.js
View File

@ -210,6 +210,12 @@ handlebars = handlebars.create({
return options.fn(this);
}
return options.inverse(this);
},
cartTotalItems: function(cart){
if(cart) {
return cart.reduce((a, b) => +a + +b.quantity, 0);
}
return 0;
}
}
});

View File

@ -107,7 +107,7 @@
{{#ifCond page '!=' 'checkout'}}
{{#ifCond page '!=' 'pay'}}
{{#if session.cart}}
<li><a href="/cart" class="menu-btn"><i class="fa fa-shopping-cart" aria-hidden="true"></i> Cart <span class="badge" id="cart-count">{{objectLength session.cart}}</span></a></li>
<li><a href="/cart" class="menu-btn"><i class="fa fa-shopping-cart" aria-hidden="true"></i> Cart <span class="badge" id="cart-count">{{cartTotalItems session.cart}}</span></a></li>
{{else}}
<li><a href="/cart" class="menu-btn"><i class="fa fa-shopping-cart" aria-hidden="true"></i> Cart <span class="badge" id="cart-count">0</span></a></li>
{{/if}}