document.addEventListener('DOMContentLoaded', function () { document.querySelectorAll('[data-cart-decrement]').forEach(function (button) { button.addEventListener('click', function () { const quantity = Number(button.dataset.currentQuantity || '0'); const removeForm = document.getElementById(button.dataset.removeFormId); const updateForm = document.getElementById(button.dataset.updateFormId); if (quantity <= 1) { if (window.confirm('Удалить товар из корзины?') && removeForm) { removeForm.submit(); } return; } if (updateForm) { updateForm.submit(); } }); }); const checkoutForm = document.querySelector('[data-checkout-form]'); if (!checkoutForm) { return; } const productsTotal = Number(checkoutForm.dataset.productsTotal || '0'); const deliveryPrice = 500; const floorLiftPrice = 45; const totalNodes = document.querySelectorAll('[data-checkout-total-text]'); const deliveryNode = document.querySelector('[data-delivery-total-text]'); const floorLiftNode = document.querySelector('[data-floor-lift-total-text]'); const formatCurrency = function (value) { return new Intl.NumberFormat('ru-RU', { minimumFractionDigits: 2, maximumFractionDigits: 2, }).format(value) + ' руб.'; }; const updateCheckoutTotals = function () { const selectedDelivery = checkoutForm.querySelector('input[name="delivery_method"]:checked'); const hasDelivery = selectedDelivery && selectedDelivery.value === 'courier'; const deliveryTotal = hasDelivery ? deliveryPrice : 0; const floorLiftTotal = hasDelivery ? floorLiftPrice : 0; const total = productsTotal + deliveryTotal + floorLiftTotal; totalNodes.forEach(function (node) { node.textContent = formatCurrency(total); }); if (deliveryNode) { deliveryNode.textContent = formatCurrency(deliveryTotal); } if (floorLiftNode) { floorLiftNode.textContent = formatCurrency(floorLiftTotal); } }; checkoutForm.querySelectorAll('input[name="delivery_method"]').forEach(function (input) { input.addEventListener('change', updateCheckoutTotals); }); updateCheckoutTotals(); });