Skip to content

Commit

Permalink
Fix cart item quantity bug
Browse files Browse the repository at this point in the history
  • Loading branch information
w3dd1e committed Nov 18, 2023
1 parent 06e90cf commit 9cd9a54
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions front/js/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,20 @@ quantitySelect.addEventListener("change", (event) => {
quantity = event.target.value;});


//Add item to cart
//Add item to cart
addToCart.addEventListener("click", (event) => {
let cartItem = JSON.stringify(item);

if (colorOption===""||quantity==="0") {
alert("Please select a color and quantity.");
}
else if (cart.hasOwnProperty(JSON.stringify(item))) {
cart[JSON.stringify(item)] = Number(cart[JSON.stringify(item)]) + Number(quantity);
//cartItem does not work with Number(), will return NaN
else if (cart.hasOwnProperty(cartItem)) {
cart[cartItem] = Number(cart[JSON.stringify(item)]) + Number(quantity);
alert("Items added to cart!");
}
else {
cart.setItem(JSON.stringify(item), quantity)
cart.setItem(cartItem, quantity)
alert("Item added to cart!");
}});

Expand Down

0 comments on commit 9cd9a54

Please sign in to comment.