© 2025 The Cozy Corner Cafe. All Rights Reserved.
@@ -227,74 +202,28 @@
});
});
- // Shopping Cart Logic
- let cart = [];
-
function addToCart(name, price) {
- // Check if item is already in cart
- const existingItem = cart.find(item => item.name === name);
- if (existingItem) {
- existingItem.quantity++;
+ let cart = JSON.parse(localStorage.getItem('cart')) || {};
+ if (cart[name]) {
+ cart[name].quantity++;
} else {
- cart.push({ name, price, quantity: 1 });
+ cart[name] = {
+ price: price,
+ quantity: 1
+ };
}
- renderCart();
+ localStorage.setItem('cart', JSON.stringify(cart));
+ updateCartCount();
}
- function removeFromCart(name) {
- const itemIndex = cart.findIndex(item => item.name === name);
- if (itemIndex > -1) {
- cart.splice(itemIndex, 1);
- }
- renderCart();
- }
-
- function updateQuantity(name, quantity) {
- const item = cart.find(item => item.name === name);
- if (item) {
- item.quantity = parseInt(quantity);
- if (item.quantity <= 0) {
- removeFromCart(name);
- } else {
- renderCart();
- }
- }
- }
-
- function renderCart() {
- const cartItemsContainer = document.getElementById('cart-items');
- const cartTotalEl = document.getElementById('cart-total');
- const cartCountEl = document.getElementById('cart-count');
- let total = 0;
- let totalItems = 0;
-
- cartItemsContainer.innerHTML = '';
-
- if (cart.length === 0) {
- cartItemsContainer.innerHTML = '
Your cart is empty.
';
- }
-
- cart.forEach(item => {
- total += item.price * item.quantity;
- totalItems += item.quantity;
- cartItemsContainer.innerHTML += `
-
-
-
${item.name}
-
-
-
- ${(item.price * item.quantity).toFixed(2)}
-
-
-
- `;
- });
-
- cartTotalEl.textContent = total.toFixed(2);
- cartCountEl.textContent = totalItems;
+ function updateCartCount() {
+ let cart = JSON.parse(localStorage.getItem('cart')) || {};
+ let count = Object.values(cart).reduce((sum, item) => sum + item.quantity, 0);
+ document.getElementById('cart-count').innerText = count;
}
+ // Update cart count on page load
+ document.addEventListener('DOMContentLoaded', updateCartCount);