diff --git a/app.js b/app.js index ed8bd96..d831e44 100644 --- a/app.js +++ b/app.js @@ -210,12 +210,6 @@ 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; } } }); diff --git a/routes/index.js b/routes/index.js index 6bdaa7a..2b1e123 100644 --- a/routes/index.js +++ b/routes/index.js @@ -284,8 +284,8 @@ router.post('/product/addtocart', (req, res, next) => { common.updateTotalCartAmount(req, res); // update how many products in the shopping cart - req.session.cartTotalItems = Object.keys(req.session.cart).length; - return res.status(200).json({message: 'Cart successfully updated', totalCartItems: Object.keys(req.session.cart).length}); + req.session.cartTotalItems = req.session.cart.reduce((a, b) => +a + +b.quantity, 0); + return res.status(200).json({message: 'Cart successfully updated', totalCartItems: req.session.cartTotalItems}); }); }); diff --git a/views/layouts/layout.hbs b/views/layouts/layout.hbs index 6ba39e5..a7d076a 100644 --- a/views/layouts/layout.hbs +++ b/views/layouts/layout.hbs @@ -107,7 +107,7 @@ {{#ifCond page '!=' 'checkout'}} {{#ifCond page '!=' 'pay'}} {{#if session.cart}} -
  • Cart {{cartTotalItems session.cart}}
  • +
  • Cart {{session.cartTotalItems}}
  • {{else}}
  • Cart 0
  • {{/if}}