I am creating a javascript cart.
Here's how I add a product to the cart:
function addToCart(id, qty){
var basket = []
basket.push({
product_id: id,
quantity: qty
});
localStorage.setItem('basket', JSON.stringify(basket));
}
Now if id is already there, I am trying only to update current basket from localStorage
When I add the same product id, it's duplicating it.
[{"product_id":"10", "quantity":"1"}, {"product_id":"10", "quantity":"1"}]
I want to increase only quantity like:
[{"product_id":"10", "quantity":"2"}]
Tried few methods with each and for with no luck :(
Any help please?