Мой веб-сайт также есть на GitHub, если кому-то понадобится. Или вы можете поискать его в Интернете и увидеть проблему. Попробуйте добавить товар, а затем откройте корзину — mimiluxm.github.io
Код: Выделить всё
const cartItemsContainer = document.querySelector('.cart-items');
const totalPriceElement = document.querySelector('.total-price');
const cartSection = document.querySelector('.cart');
const checkoutForm = document.querySelector('.checkout-form');
let cartItems = JSON.parse(localStorage.getItem('cartItems')) || [];
function updateCart() {
cartItemsContainer.innerHTML = '';
let total = 0;
cartItems.forEach((item, index) => {
console.log(item);
const cartItem = document.createElement('div');
cartItem.classList.add('cart-item');
cartItem.innerHTML = `
[img]${item.image}[/img]
${item.name}
${item.price} MDL
-
`;
cartItemsContainer.appendChild(cartItem);
total += item.price;
});
totalPriceElement.textContent = `Total: ${total} MDL`;
}
function removeItem(index) {
cartItems.splice(index, 1);
updateCart();
localStorage.setItem('cartItems', JSON.stringify(cartItems));
}
function openCheckout() {
if (cartItems.length === 0) {
alert('Coșul este gol! Adăugați produse înainte de a finaliza comanda.');
return;
}
cartSection.style.display = 'none';
checkoutForm.style.display = 'block';
}
function submitOrder() {
const firstName = document.getElementById('first-name').value;
const lastName = document.getElementById('last-name').value;
const phone = document.getElementById('phone').value;
const email = document.getElementById('email').value;
const address = document.getElementById('address').value;
if (!firstName || !lastName || !phone || !email || !address) {
alert('Vă rugăm să completați toate câmpurile!');
return;
}
const total = cartItems.reduce((sum, item) => sum + item.price, 0);
alert(`Vă mulțumim pentru comandă, ${firstName} ${lastName}!\nTotal: ${total} MDL.\nVă vom contacta la ${phone} sau prin email la ${email}.\nAdresa de livrare: ${address}`);
cartItems.length = 0;
updateCart();
localStorage.setItem('cartItems', JSON.stringify(cartItems));
checkoutForm.style.display = 'none';
cartSection.style.display = 'block';
}
# updateCart();
Подробнее здесь: https://stackoverflow.com/questions/793 ... nt-showing
Мобильная версия