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

master
Marty Sloan 2018-06-21 20:12:28 -04:00 committed by Mark Moffat
parent 9662d341c8
commit f9b5a84fb8
3 changed files with 3 additions and 9 deletions

6
app.js
View File

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

View File

@ -284,8 +284,8 @@ router.post('/product/addtocart', (req, res, next) => {
common.updateTotalCartAmount(req, res); common.updateTotalCartAmount(req, res);
// update how many products in the shopping cart // update how many products in the shopping cart
req.session.cartTotalItems = 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: Object.keys(req.session.cart).length}); return res.status(200).json({message: 'Cart successfully updated', totalCartItems: req.session.cartTotalItems});
}); });
}); });

View File

@ -107,7 +107,7 @@
{{#ifCond page '!=' 'checkout'}} {{#ifCond page '!=' 'checkout'}}
{{#ifCond page '!=' 'pay'}} {{#ifCond page '!=' 'pay'}}
{{#if session.cart}} {{#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">{{cartTotalItems 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">{{session.cartTotalItems}}</span></a></li>
{{else}} {{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> <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}} {{/if}}