/** * Cart Drawer Component * Manages the cart sidebar with add/remove/update functionality */ (function() { const drawer = document.getElementById('cart-drawer'); const emptyState = document.querySelector('[data-cart-empty]'); const itemsContainer = document.querySelector('[data-cart-items]'); const checkoutBtn = document.querySelector('[data-cart-checkout]'); const closeButtons = document.querySelectorAll('[data-cart-close]'); const totalDisplay = document.querySelector('[data-cart-total]'); const headerCartBtn = document.querySelector('[data-cart-open]'); const headerCartCount = document.querySelector('[data-cart-count]'); // Get translated text const removeText = drawer.dataset.textRemove || 'Remove'; let currentCart = null; let cartInstance = null; // Wait for ShopifyCart to be available function initCartDrawer() { if (typeof ShopifyCart === 'undefined') { setTimeout(initCartDrawer, 100); return; } cartInstance = new ShopifyCart({ domain: 'nv7cqv-bu.myshopify.com', storefrontAccessToken: 'dec3d35a2554384d149c72927d1cfd1b' }); // Initialize event listeners setupEventListeners(); } function setupEventListeners() { // Close drawer closeButtons.forEach(btn => { btn.addEventListener('click', closeDrawer); }); // Open drawer from header button if (headerCartBtn) { headerCartBtn.addEventListener('click', openDrawer); } // Checkout button checkoutBtn.addEventListener('click', () => { if (currentCart?.checkoutUrl) { window.location.href = currentCart.checkoutUrl; } }); // Listen for custom cart update events document.addEventListener('cart:updated', (e) => { currentCart = e.detail.cart; renderCart(); openDrawer(); }); } function openDrawer() { drawer.classList.add('is-open'); document.body.style.overflow = 'hidden'; } function closeDrawer() { drawer.classList.remove('is-open'); document.body.style.overflow = ''; } function calculateTotal() { if (!currentCart || !currentCart.lines) return 0; return currentCart.lines.edges.reduce((total, edge) => { const item = edge.node; const price = parseFloat(item.merchandise.price.amount); const quantity = item.quantity; return total + (price * quantity); }, 0); } function formatPrice(amount, currency = 'EUR') { return new Intl.NumberFormat('fr-FR', { style: 'currency', currency: currency }).format(amount); } function updateCartCount() { if (!currentCart || !currentCart.lines || currentCart.lines.edges.length === 0) { if (headerCartCount) { headerCartCount.textContent = ''; } return; } // Calculate total quantity const totalQty = currentCart.lines.edges.reduce((sum, edge) => { return sum + edge.node.quantity; }, 0); if (headerCartCount) { headerCartCount.textContent = totalQty > 0 ? totalQty : ''; } } function renderCart() { if (!currentCart || !currentCart.lines || currentCart.lines.edges.length === 0) { emptyState.classList.remove('hidden'); itemsContainer.classList.add('hidden'); checkoutBtn.disabled = true; if (totalDisplay) { totalDisplay.textContent = '0,00 €'; } updateCartCount(); return; } emptyState.classList.add('hidden'); itemsContainer.classList.remove('hidden'); checkoutBtn.disabled = false; // Calculate and display total const total = calculateTotal(); const currency = currentCart.lines.edges[0]?.node.merchandise.price.currencyCode || 'EUR'; if (totalDisplay) { totalDisplay.textContent = formatPrice(total, currency); } // Update header cart count updateCartCount(); // Render cart items itemsContainer.innerHTML = currentCart.lines.edges.map(edge => { const item = edge.node; const merchandise = item.merchandise; return `
${merchandise.title}
` : ''}${formatPrice(parseFloat(merchandise.price.amount), merchandise.price.currencyCode)}