// Shopping Cart and Checkout System for SlimCore Weight Loss let cart = []; // DOM Elements let cartCountEl; let cartItemsEl; let cartEmptyEl; let cartSummaryEl; let cartSubtotalEl; let cartTotalEl; let checkoutBtnEl; let checkoutModal; let closeCheckoutEl; let checkoutForm; let checkoutItemsEl; let checkoutSubtotalEl; let checkoutTotalEl; // Initialize cart functionality function initializeCart() { cartCountEl = document.getElementById('cart-count'); cartItemsEl = document.getElementById('cart-items'); cartEmptyEl = document.getElementById('cart-empty'); cartSummaryEl = document.getElementById('cart-summary'); cartSubtotalEl = document.getElementById('cart-subtotal'); cartTotalEl = document.getElementById('cart-total'); checkoutBtnEl = document.getElementById('checkout-btn'); checkoutModal = document.getElementById('checkout-modal'); closeCheckoutEl = document.getElementById('close-checkout'); checkoutForm = document.getElementById('checkout-form'); checkoutItemsEl = document.getElementById('checkout-items'); checkoutSubtotalEl = document.getElementById('checkout-subtotal'); checkoutTotalEl = document.getElementById('checkout-total'); // Add event listeners for "Add to Cart" buttons const addToCartButtons = document.querySelectorAll('.add-to-cart'); addToCartButtons.forEach(button => { button.addEventListener('click', handleAddToCart); }); // Checkout button event listener if (checkoutBtnEl) { checkoutBtnEl.addEventListener('click', openCheckoutModal); } // Close checkout modal if (closeCheckoutEl) { closeCheckoutEl.addEventListener('click', closeCheckoutModal); } // Close modal when clicking outside if (checkoutModal) { checkoutModal.addEventListener('click', function(e) { if (e.target === checkoutModal) { closeCheckoutModal(); } }); } // Form submission if (checkoutForm) { checkoutForm.addEventListener('submit', handleCheckoutSubmission); } updateCartDisplay(); } // Handle adding items to cart function handleAddToCart(e) { const button = e.target; const program = button.getAttribute('data-program'); const name = button.getAttribute('data-name'); const price = parseInt(button.getAttribute('data-price')); const weeks = button.getAttribute('data-weeks'); // Check if item already exists in cart const existingItem = cart.find(item => item.program === program); if (existingItem) { // Update quantity if item exists existingItem.quantity += 1; } else { // Add new item to cart cart.push({ program: program, name: name, price: price, weeks: weeks, quantity: 1 }); } // Visual feedback button.innerHTML = ' Added!'; button.style.backgroundColor = '#10B981'; setTimeout(() => { button.innerHTML = 'Add to Cart'; button.style.backgroundColor = ''; }, 2000); updateCartDisplay(); } // Update cart display function updateCartDisplay() { const itemCount = cart.reduce((total, item) => total + item.quantity, 0); const subtotal = cart.reduce((total, item) => total + (item.price * item.quantity), 0); // Update cart count if (cartCountEl) { cartCountEl.textContent = itemCount; } // Show/hide empty state and summary if (cart.length === 0) { if (cartEmptyEl) cartEmptyEl.style.display = 'block'; if (cartSummaryEl) cartSummaryEl.style.display = 'none'; if (cartItemsEl) { cartItemsEl.innerHTML = `
Your cart is empty
Select a program above to get started
${item.weeks}-week supply
${item.weeks}-week supply × ${item.quantity}