Skip to content

Commit

Permalink
Update menu.js - Streamline add cart process
Browse files Browse the repository at this point in the history
Modified the existing code to store the saved items in an array to streamline the process.
  • Loading branch information
rees8 authored Oct 1, 2024
1 parent 43bb258 commit 7728626
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ document.addEventListener('DOMContentLoaded', function () {
var itemName = item.querySelector('h3').textContent;
var itemPrice = item.querySelector('p').textContent;
alert("Item added to cart successfully");

// Retrieve existing cart items from localStorage
var cartItems = JSON.parse(localStorage.getItem('cartItems')) || [];

// Add new item to the cart array
var newItem = { name: itemName, price: itemPrice };
cartItems.push(newItem);

localStorage.setItem('itemName', itemName);
localStorage.setItem('itemPrice', itemPrice);
// Save updated cart back to localStorage
localStorage.setItem('cartItems', JSON.stringify(cartItems));

window.location.href = "cart.html";
}
Expand Down

0 comments on commit 7728626

Please sign in to comment.