// JavaScript блок (добавьте в HTML-код перед
Количество | Молочный | Темный | Горький |
---|---|---|---|
100-499 шт | 25.00₽ | 28.00₽ | 35.00₽ |
500-999 шт | 17.00₽ | 20.00₽ | 27.00₽ |
1000-1999 шт | 15.00₽ | 18.00₽ | 25.00₽ |
2000-2999 шт | 14.60₽ | 17.60₽ | 24.60₽ |
3000-4999 шт | 14.25₽ | 17.25₽ | 24.25₽ |
5000-9999 шт | 14.10₽ | 17.10₽ | 24.10₽ |
от 10000 шт | 14.00₽ | 17.00₽ | 24.00₽ |
)
document.addEventListener("DOMContentLoaded", function() {
// Ценовые уровни
const priceLevels = [
{minQty: 100, milk: 25, dark: 28, bitter: 35},
{minQty: 500, milk: 17, dark: 20, bitter: 27},
{minQty: 1000, milk: 15, dark: 18, bitter: 25},
{minQty: 2000, milk: 14.6, dark: 17.6, bitter: 24.6},
{minQty: 3000, milk: 14.25, dark: 17.25, bitter: 24.25},
{minQty: 5000, milk: 14.1, dark: 17.1, bitter: 24.1},
{minQty: 10000, milk: 14, dark: 17, bitter: 24}
];
// Текущий выбор
let selectedType = 'milk';
let quantity = 100;
let currentPriceLevel = priceLevels[0];
// Элементы
const typeButtons = document.querySelectorAll('.type-options button');
const qtyInput = document.getElementById('product-qty');
const qtyMinus = document.querySelector('.qty-minus');
const qtyPlus = document.querySelector('.qty-plus');
const unitPriceEl = document.getElementById('unit-price');
const totalPriceEl = document.getElementById('total-price');
const notification = document.getElementById('cart-notification');
// Функция расчета цены
function calculatePrice() {
// Находим подходящий ценовой уровень
let newPriceLevel = priceLevels[0];
for (let i = priceLevels.length - 1; i >= 0; i--) {
if (quantity >= priceLevels[i].minQty) {
newPriceLevel = priceLevels[i];
break;
}
}
// Обновляем текущий уровень
currentPriceLevel = newPriceLevel;
// Получаем цену для выбранного типа
const unitPrice = currentPriceLevel[selectedType];
const totalPrice = unitPrice * quantity;
// Обновляем отображение
unitPriceEl.textContent = unitPrice.toFixed(2);
totalPriceEl.textContent = totalPrice.toFixed(2);
// Обновляем подсветку в таблице
updatePriceTableHighlight();
}
// Обновление подсветки таблицы
function updatePriceTableHighlight() {
// Убираем подсветку со всех строк
document.querySelectorAll('.price-table tr').forEach(row => {
row.classList.remove('highlighted');
});
// Находим и подсвечиваем нужную строку
const rows = document.querySelectorAll('.price-table tr[data-min]');
for (const row of rows) {
const rowMin = parseInt(row.dataset.min);
if (quantity >= rowMin && rowMin === currentPriceLevel.minQty) {
row.classList.add('highlighted');
break;
}
}
}
// Выбор типа шоколада
typeButtons.forEach(button => {
button.addEventListener('click', () => {
typeButtons.forEach(btn => btn.classList.remove('active'));
button.classList.add('active');
selectedType = button.dataset.type;
calculatePrice();
});
});
// Изменение количества
qtyInput.addEventListener('input', () => {
quantity = Math.max(100, parseInt(qtyInput.value) || 100);
qtyInput.value = quantity;
calculatePrice();
});
// Кнопки +/-
qtyMinus.addEventListener('click', () => {
quantity = Math.max(100, quantity - 100);
qtyInput.value = quantity;
calculatePrice();
});
qtyPlus.addEventListener('click', () => {
quantity += 100;
qtyInput.value = quantity;
calculatePrice();
});
// Кнопка добавления в корзину
document.querySelector('.add-to-cart').addEventListener('click', () => {
// ЗАМЕНИТЕ 123456 НА РЕАЛЬНЫЙ ID ТОВАРА В TILDA
const productId = 123456;
// Получаем параметры
const typeName = getTypeName(selectedType);
const quantityVal = parseInt(qtyInput.value);
const unitPrice = parseFloat(unitPriceEl.textContent);
const totalPrice = parseFloat(totalPriceEl.textContent);
// Добавляем в корзину Tilda
window.tilda_cart_add(
productId,
quantityVal,
{
"Тип шоколада": typeName,
"Цена за единицу": unitPrice.toFixed(2) + "₽"
},
`${typeName} шоколад (${quantityVal} шт)`,
totalPrice
);
// Показываем уведомление
showNotification(`✅ Добавлено ${quantityVal} шт ${typeName} шоколада!
Сумма: ${totalPrice.toFixed(2)}₽`);
// Обновляем счетчик корзины
updateCartCounter();
});
// Вспомогательные функции
function getTypeName(type) {
const names = {
milk: "Молочный",
dark: "Темный",
bitter: "Горький"
};
return names[type] || type;
}
function showNotification(message) {
notification.innerHTML = message;
notification.classList.add('show');
setTimeout(() => {
notification.classList.remove('show');
}, 3000);
}
function updateCartCounter() {
const cartCount = document.querySelector('.t706__carticon-count');
if(cartCount) {
const currentCount = parseInt(cartCount.textContent) || 0;
cartCount.textContent = currentCount + 1;
// Анимация счетчика
cartCount.style.transform = 'scale(1.5)';
setTimeout(() => {
cartCount.style.transform = 'scale(1)';
}, 300);
}
}
// Инициализация
calculatePrice();
});