function countDeliverySum() {
const subTotalSum = document.querySelector('.cart-subtotal .woocommerce-Price-amount bdi');
const subTotalSumOnlyNum = +subTotalSum.innerHTML.replace(/\D/g, '');
const totalSum = document.querySelector('.order-total .woocommerce-Price-amount bdi');
const deliveryTypes = document.querySelectorAll('.class_delivery input[type="radio"]');
const locationRadios = document.querySelectorAll('.location_delivery input[type="radio"]');
function calculateTotal(deliveryPrice) {
totalSum.innerHTML = `${subTotalSumOnlyNum + deliveryPrice} ₽`;
}
function activateDeliveryTypes(...indexes) {
deliveryTypes.forEach((type, index) => type.classList.toggle('active', indexes.includes(index)));
}
locationRadios.forEach((locationRadio) => {
locationRadio.addEventListener('click', () => {
if (locationRadio.id === 'location_moscow') {
activateDeliveryTypes(0, 1, 2, 3, 4);
calculateTotal(390);
} else if (locationRadio.id === 'location_other_city') {
activateDeliveryTypes(0, 1);
activateDeliveryTypes(2, 3, 4, 5, 6);
calculateTotal(1190);
} else {
activateDeliveryTypes(0, 1, 2, 3, 5, 6);
calculateTotal(3500);
}
});
});
}
function postData(form) {
form.addEventListener("submit", (e) => {
e.preventDefault();
const statusMessage = document.createElement("div");
statusMessage.classList.add("status");
statusMessage.textContent = message.loading;
form.append(statusMessage);
const request = new XMLHttpRequest();
request.open("POST", "server.php");
request.setRequestHeader("Content-type", "application/json");
const formData = new FormData(form);
const object = {};
formData.forEach(function(value, key){
object[key] = value;
});
const jsonData = JSON.stringify(object);
request.send(jsonData);
request.addEventListener("load", () => {
if (request.status === 200) {
console.log(request.response);
statusMessage.textContent = message.success;
} else {
statusMessage.textContent = message.failure;
}
});
});
}