<script>
document.addEventListener("DOMContentLoaded", function(){
let delivPositionPrice = {
1: 400,
2: 200
};
let weightDiv = '';
function ChangeDelivery(){
let weight=0;
let cart = document.querySelectorAll('div[data-record-type="706"]');
if(cart.length){
if(tcart.total){
for (let i = 0; i < tcart.products.length; i++) {
weight += (+tcart.products[i].pack_m * tcart.products[i].quantity)/1000;
};
weight = +weight.toFixed(2);
weightDiv.innerHTML = "Вес: "+weight+" кг";
document.querySelectorAll('.t706__cartwin-prodamount-wrap')[0].append(weightDiv);
let delivInp = document.querySelectorAll('.t706 .t-radio__wrapper-delivery input');
for (let i = 0; i < delivInp.length; i++) {
if(delivPositionPrice[i+1] != undefined){
delivInp[i].setAttribute("data-delivery-price", (delivPositionPrice[i+1]*weight).toFixed(2) );
};
}; tcart__updateDelivery();
};
};
};
function startChDeliv(){
ChangeDelivery();
setTimeout(function(){ ChangeDelivery()}, 4500);
};
//При открытии страницы
setTimeout(function(){
weightDiv = document.createElement('div');
weightDiv.className = "t706__cartwin-weight";
startChDeliv();
}, 1500);
//При нажатии на кнопку корзину
let cartIcon = document.querySelectorAll('.t706__carticon');
cartIcon.forEach(i => i.addEventListener('click', startChDeliv ) );
//При нажатии на минус, плюс и удаление в корзине
let cartProd = document.querySelectorAll('.t706__cartwin-products');
cartProd.forEach(i => i.addEventListener('click', startChDeliv ) );
//При потере фокуса в поле количества в корзине
let cartQ = document.querySelectorAll('.t706__cartwin-products');
for (let i = 0; i < cartQ.length; i++) {
cartQ[i].addEventListener('input', function() {
setTimeout(function(){
let cartQinp = document.querySelectorAll('.t706__cartwin-products input');
cartQinp.forEach(i => i.addEventListener('focusout', startChDeliv ) );
}, 500);
});
};
//При нажатии на кнопку #order , minus, plus, delete
document.addEventListener('click', function(e){
let el = false; let link = e.target.getAttribute('href');
if (link != null) { if(link.includes('#order')) el = true;
}else{ if( e.target.closest('a[href^="#order"]') !=null ) el = true };
if(el) startChDeliv();
});
});
</script>