You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
4.3 KiB
105 lines
4.3 KiB
function handleOrderForm(formElement) {
|
|
if (!formElement) return;
|
|
|
|
const formData = new FormData(formElement);
|
|
const submitButton = formElement.querySelector('.js-cart__popup-submit');
|
|
const cartPopup = document.querySelector('.cart-popup__wrapper');
|
|
|
|
if (submitButton) {
|
|
submitButton.disabled = true;
|
|
submitButton.textContent = 'Оформляем...';
|
|
submitButton.classList.add('is-loading');
|
|
}
|
|
|
|
fetch('/local/ajax/order.php', {
|
|
method: 'POST',
|
|
body: formData
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
// Показываем сообщение об успехе
|
|
if (typeof showCartToast === 'function') {
|
|
showCartToast("Заказ успешно оформлен! Номер заказа: " + data.order_id, 5000, 'success');
|
|
} else {
|
|
alert("Заказ успешно оформлен! Номер заказа: " + data.order_id);
|
|
}
|
|
|
|
// Закрываем попап
|
|
if (cartPopup) {
|
|
cartPopup.classList.add("cart-popup__wrapper--hidden");
|
|
}
|
|
|
|
// Очищаем форму
|
|
formElement.reset();
|
|
|
|
// Перенаправляем на страницу заказа через 2 секунды
|
|
if (data.redirect) {
|
|
setTimeout(() => {
|
|
window.location.href = data.redirect;
|
|
}, 2000);
|
|
}
|
|
|
|
// Обновляем счетчик корзины
|
|
updateCartCounter();
|
|
|
|
} else {
|
|
if (typeof showCartToast === 'function') {
|
|
showCartToast(data.message || "Ошибка при оформлении заказа", 3000, 'error');
|
|
} else {
|
|
alert(data.message || "Ошибка при оформлении заказа");
|
|
}
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Ошибка:', error);
|
|
if (typeof showCartToast === 'function') {
|
|
showCartToast("Произошла ошибка при отправке", 3000, 'error');
|
|
} else {
|
|
alert("Произошла ошибка при отправке");
|
|
}
|
|
})
|
|
.finally(() => {
|
|
if (submitButton) {
|
|
submitButton.disabled = false;
|
|
submitButton.textContent = 'Оформить заказ';
|
|
submitButton.classList.remove('is-loading');
|
|
}
|
|
});
|
|
}
|
|
|
|
// Функция для обновления счетчика корзины
|
|
function updateCartCounter() {
|
|
const cartCounter = document.querySelector('.cart-counter');
|
|
if (cartCounter) {
|
|
cartCounter.textContent = '0';
|
|
cartCounter.style.display = 'none';
|
|
}
|
|
|
|
// Обновляем содержимое корзины если находимся на странице корзины
|
|
if (window.location.pathname.includes('/cart/')) {
|
|
location.reload();
|
|
}
|
|
}
|
|
|
|
// Инициализация при загрузке DOM
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
// Находим форму оформления заказа
|
|
const orderForm = document.getElementById('cart-form');
|
|
|
|
if (orderForm) {
|
|
// Заменяем стандартный обработчик
|
|
orderForm.addEventListener('submit', function(e) {
|
|
e.preventDefault();
|
|
handleOrderForm(this);
|
|
});
|
|
|
|
// Убираем старый обработчик с кнопки
|
|
const submitBtn = orderForm.querySelector('.js-cart__popup-submit');
|
|
if (submitBtn) {
|
|
// Клонируем элемент чтобы удалить все старые обработчики
|
|
const newSubmitBtn = submitBtn.cloneNode(true);
|
|
submitBtn.parentNode.replaceChild(newSubmitBtn, submitBtn);
|
|
}
|
|
}
|
|
});
|