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
parent
9662d341c8
commit
f9b5a84fb8
6
app.js
6
app.js
|
@ -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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -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});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -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}}
|
||||||
|
|
Loading…
Reference in New Issue