let cart = JSON.parse(localStorage.getItem("aavas_cart") || "[]");
function displayProducts(category = "All") {
const container = document.getElementById("product-list");
const filtered = category === "All"
? products
: products.filter(product => product.category === category);
if (!filtered.length) {
container.innerHTML = "
No products available in this category yet.
";
return;
}
container.innerHTML = filtered.map(product => `
${product.name}
${product.description}
JMD $${Number(product.price).toLocaleString("en-JM")}
`).join("");
}
function filterProducts(category) {
displayProducts(category);
}
function addToCart(id) {
const product = products.find(item => item.id === id);
if (!product) return;
const existing = cart.find(item => item.id === id);
if (existing) {
existing.quantity++;
} else {
cart.push({ ...product, quantity: 1 });
}
updateCart();
// No annoying alert anymore - cart count updates at top
}
function updateCart() {
const count = cart.reduce((sum, item) => sum + item.quantity, 0);
document.getElementById("cart-count").textContent = count;
const items = document.getElementById("cart-items");
if (cart.length === 0) {
items.innerHTML = "Your cart is empty.
";
} else {
items.innerHTML = cart.map(item => `
${item.name}
JMD $${(item.price * item.quantity).toLocaleString("en-JM")}
${item.quantity}
`).join("");
}
const total = cart.reduce(
(sum, item) => sum + item.price * item.quantity, 0
);
document.getElementById("cart-total").textContent =
"Total: JMD $" + total.toLocaleString("en-JM");
// Save cart so it doesn't disappear on refresh
localStorage.setItem("aavas_cart", JSON.stringify(cart));
}
function changeQty(id, change) {
const item = cart.find(i => i.id === id);
if (!item) return;
item.quantity += change;
if (item.quantity <= 0) {
cart = cart.filter(i => i.id !== id);
}
updateCart();
}
function removeFromCart(id) {
cart = cart.filter(item => item.id !== id);
updateCart();
}
function showCart() {
document.getElementById("cart-modal").style.display = "block";
updateCart();
}
function hideCart() {
document.getElementById("cart-modal").style.display = "none";
}
function checkout() {
if (cart.length === 0) {
alert("Your cart is empty. Please add products first.");
return;
}
const total = cart.reduce(
(sum, item) => sum + item.price * item.quantity, 0
);
let message = "Hello Aava's Promise! I would like to place an order:\n\n";
cart.forEach(item => {
message += item.name + " x " + item.quantity +
" = JMD $" + (item.price * item.quantity).toLocaleString("en-JM") + "\n";
});
message += "\nTotal: JMD $" + total.toLocaleString("en-JM");
message += "\n\nPlease confirm availability, delivery fee and payment instructions.";
message += "\n\nName:\nLocation:\nPayment Method:";
window.open(
"https://wa.me/18762247815?text=" + encodeURIComponent(message),
"_blank"
);
}
document.addEventListener("DOMContentLoaded", () => {
displayProducts();
updateCart();
// Close cart when clicking outside the white box
document.getElementById("cart-modal").addEventListener("click", (e) => {
if (e.target.id === "cart-modal") hideCart();
});
});