const CART_KEY = 'mmb_cart'; function getCart() { try { return JSON.parse(localStorage.getItem(CART_KEY)) || []; } catch { return []; } } function saveCart(cart) { localStorage.setItem(CART_KEY, JSON.stringify(cart)); updateCartCount(); } function updateCartCount() { const count = getCart().reduce((sum, item) => sum + item.quantity, 0); const node = document.getElementById('cart-count'); if (node) node.textContent = count; } function addToCart(productId, quantity) { const qty = Number(quantity || 1); if (qty <= 0) return; const product = window.PRODUCTS.find(p => p.id === productId); if (!product) return; const cart = getCart(); const existing = cart.find(item => item.id === productId); if (existing) existing.quantity += qty; else cart.push({ id: product.id, name: product.name, price: product.price, quantity: qty }); saveCart(cart); alert(`${product.name} added to cart.`); } function clearCart() { localStorage.removeItem(CART_KEY); updateCartCount(); renderCheckout(); } function formatMoney(value) { return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(value); } function calculateTotals(cart) { const subtotal = cart.reduce((sum, item) => sum + (item.price * item.quantity), 0); const discount = subtotal >= 100 ? subtotal * 0.10 : 0; return { subtotal, discount, total: subtotal - discount }; } function productCard(product) { return `
${product.category}

${product.name}

${formatMoney(product.price)}

${product.description}

Priced per ${product.unit}.

`; } function renderProducts() { const productGrid = document.getElementById('product-grid'); if (productGrid) productGrid.innerHTML = window.PRODUCTS.map(productCard).join(''); const featured = document.getElementById('featured-products'); if (featured) featured.innerHTML = window.PRODUCTS.slice(0, 3).map(productCard).join(''); } function renderCheckout() { const cart = getCart(); const cartItems = document.getElementById('cart-items'); if (!cartItems) return; if (!cart.length) { cartItems.innerHTML = '

Your cart is empty.

'; } else { cartItems.innerHTML = cart.map(item => `
${item.name}
Qty ${item.quantity}
${formatMoney(item.price * item.quantity)}
`).join(''); } const totals = calculateTotals(cart); document.getElementById('subtotal').textContent = formatMoney(totals.subtotal); document.getElementById('discount').textContent = `-${formatMoney(totals.discount)}`; document.getElementById('total').textContent = formatMoney(totals.total); } function initCheckoutForm() { const form = document.getElementById('checkout-form'); if (!form) return; form.addEventListener('submit', (event) => { event.preventDefault(); const cart = getCart(); const result = document.getElementById('order-result'); if (!cart.length) { result.classList.remove('hidden'); result.innerHTML = 'Your cart is empty. Add products before checking out.'; return; } const formData = new FormData(form); const values = Object.fromEntries(formData.entries()); const totals = calculateTotals(cart); const orderLines = cart.map(item => `• ${item.name} x ${item.quantity} = ${formatMoney(item.price * item.quantity)}`).join('
'); const message = `

Order request generated

Name: ${values.name}
Phone: ${values.phone}
Email: ${values.email}
Pickup date: ${values.pickupDate}
Method: ${values.fulfillment}
Location: ${values.location || 'Not provided'}
Notes: ${values.notes || 'None'}

Items
${orderLines}

Total: ${formatMoney(totals.total)}

Next step: connect this form to Square, GoDaddy Commerce, Formspree, or your email to accept live submissions and payments.

`; result.classList.remove('hidden'); result.innerHTML = message; result.scrollIntoView({ behavior: 'smooth' }); }); const clearButton = document.getElementById('clear-cart'); if (clearButton) clearButton.addEventListener('click', clearCart); } document.addEventListener('DOMContentLoaded', () => { updateCartCount(); renderProducts(); renderCheckout(); initCheckoutForm(); });