From f9b5a84fb87020ac1cfb8943f89e47d0f6df3222 Mon Sep 17 00:00:00 2001 From: Marty Sloan Date: Thu, 21 Jun 2018 20:12:28 -0400 Subject: [PATCH] updated cartTotalItems in routes/index.js instead of adding handlebar helper. fixes quantity discrepency in the view when you first add items to the cart --- app.js | 6 ------ routes/index.js | 4 ++-- views/layouts/layout.hbs | 2 +- 3 files changed, 3 insertions(+), 9 deletions(-) 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}}