<!--КОД ДЛЯ КОРРЕКЦИИ СТОИМОСТИ ДОСТАВКИ ОТ региона и СУММЫ ЗАКАЗА-->
<script>
(function () {
const regionCity = 'Москва';
const regionArea = 'Московская область';
const deliveryCity = 'Москве';
const deliveryArea = 'Московской области';
const allArea = 'Россия';
const exceptDelivery = 'Самовывоз';
let currency = '';
t_onReady(function() {
setTimeout(function() {
t_onFuncLoad('tcart__init',function() {
showCorrectDelivery();
});
},200);
});
function showCorrectDelivery(){
const cart = document.querySelector('.t706');
currency = tcart.currency;
if(cart){
const wrapperDelivery = cart.querySelector('.t-radio__wrapper-delivery');
if(wrapperDelivery){
let timeoutId;
let observer = new MutationObserver(function(mutations) {
clearTimeout(timeoutId);
timeoutId = setTimeout(function(){
//Получаем запись о регионе
const region = wrapperDelivery.querySelector('#city-searchbox .t-input-description')?.textContent;
if(region){
processDeliveryOptions(region, wrapperDelivery);
console.log(region);
};
const deliveryLabel = wrapperDelivery.querySelectorAll('label');
deliveryLabel.forEach(label => {
label.classList.add('show-label');
});
}, 300);
});
observer.observe(wrapperDelivery, { childList: true, subtree: true});
}
}
};
function processDeliveryOptions(region, wrapperDelivery) {
const deliveryLabels = wrapperDelivery.querySelectorAll('.t-radio__control');
const labelsArray = Array.from(deliveryLabels);
if (region.includes(regionCity)) {
//Если regionCity встречается в region
processCityRegion(labelsArray);
} else if (region.includes(regionArea)) {
//Если regionArea встречается в region
processAreaRegion(labelsArray);
} else {
//ни regionArea, ни regionCity не встречаются в region
processOtherRegion(labelsArray);
}
// Проверяем, есть ли выбранный элемент среди оставшихся
checkAndSelectFirstOption(wrapperDelivery);
}
function processCityRegion(labelsArray) {
labelsArray.forEach(label => {
const input = label.querySelector('input.t-radio_delivery');
if (input) {
const value = input.value;
// Проверяем, есть ли в value слово deliveryCity или exceptDelivery
if (!value.includes(deliveryCity) && !value.includes(exceptDelivery)) {
label.remove();
}
}
});
updateDeliveryPrices(regionCity, labelsArray);
}
function processAreaRegion(labelsArray) {
labelsArray.forEach(label => {
const input = label.querySelector('input.t-radio_delivery');
if (input) {
const value = input.value;
// Проверяем, есть ли в value слово deliveryArea или exceptDelivery
if (!value.includes(deliveryArea) && !value.includes(exceptDelivery)) {
label.remove();
}
}
});
updateDeliveryPrices(regionArea, labelsArray);
}
function processOtherRegion(labelsArray) {
labelsArray.forEach(label => {
const input = label.querySelector('input.t-radio_delivery');
if (input) {
const value = input.value;
// Проверяем, есть ли в value слово deliveryArea или deliveryCity
if (value.includes(deliveryArea) || value.includes(deliveryCity)) {
label.remove();
}
}
});
updateDeliveryPrices(allArea, labelsArray);
}
function checkAndSelectFirstOption(wrapperDelivery) {
const remainingLabels = wrapperDelivery.querySelectorAll('.t-radio__control');
const remainingInputs = wrapperDelivery.querySelectorAll('input.t-radio_delivery');
// Проверяем, есть ли выбранный элемент
let hasChecked = false;
remainingInputs.forEach(input => {
if (input.checked) {
hasChecked = true;
}
});
// Если нет выбранного элемента, выбираем первый
if (!hasChecked && remainingInputs.length > 0) {
remainingInputs[0].checked = true;
remainingInputs[0].dispatchEvent(new Event('change', { bubbles: true }));
}
}
function updateDeliveryPrices(region, labelsArray) {
let total = tcart.prodamount_withdiscount || tcart.prodamount_withdyndiscount || tcart.prodamount;
const priceTable = document.querySelector('.uc-delivery-table');
if (!priceTable) return;
const tbody = priceTable.querySelector('tbody');
if (!tbody) return;
// Получаем все строки для нужного региона
const rows = Array.from(tbody.querySelectorAll('tr')).filter(row => {
const firstTd = row.querySelector('td:first-child');
return firstTd && firstTd.textContent.trim() === region;
});
let point = 0;
let door = 0;
// Находим подходящую строку по сумме заказа
for (let row of rows) {
const tds = row.querySelectorAll('td');
if (tds.length < 4) continue;
const sumText = tds[1].textContent.trim();
const pointText = tds[2].textContent.trim();
const doorText = tds[3].textContent.trim();
let isMatch = false;
if (sumText.includes('до') && !sumText.includes('от')) {
// "до XXXX"
const max = extractNumber(sumText);
if (total < max) {
isMatch = true;
}
} else if (sumText.includes('от') && sumText.includes('до')) {
// "от XXXX до YYYY"
const [min, max] = extractRangeNumbers(sumText);
if (total >= min && total < max) {
isMatch = true;
}
} else if (sumText.includes('от') && !sumText.includes('до')) {
// "от XXXX"
const min = extractNumber(sumText);
if (total >= min) {
isMatch = true;
}
}
if (isMatch) {
// point
if (pointText.includes('%')) {
const percent = parseFloat(pointText.replace('%', '').trim());
point = total * percent / 100;
} else {
point = parseFloat(pointText.replace(/\s/g, ''));
}
// door
if (doorText.includes('%')) {
const percent = parseFloat(doorText.replace('%', '').trim());
door = total * percent / 100;
} else {
door = parseFloat(doorText.replace(/\s/g, ''));
}
break;
}
}
// Форматируем цены для отображения
const formattedPoint = formatNumber(point);
const formattedDoor = formatNumber(door);
// Обновляем цены в оставшихся labels
const remainingLabels = document.querySelectorAll('#delivery-services-wrapper .t-radio__control:not([style*="display: none"])');
remainingLabels.forEach(label => {
const input = label.querySelector('input.t-radio_delivery');
if (!input) return;
const value = input.value;
const priceSpan = label.querySelector('span.delivery-minimum-price');
if (value.includes('пункт') && priceSpan) {
input.setAttribute('data-delivery-price', point);
input.setAttribute('data-static-price', point);
// Устанавливаем текст и атрибут data-text-price
priceSpan.textContent = formattedPoint + ' ' + currency;
priceSpan.setAttribute('data-text-price', formattedPoint + ' ' + currency);
} else if (value.includes('двер') && priceSpan) {
input.setAttribute('data-delivery-price', door);
input.setAttribute('data-static-price', door);
// Устанавливаем текст и атрибут data-text-price
priceSpan.textContent = formattedDoor + ' ' + currency;
priceSpan.setAttribute('data-text-price', formattedDoor + ' ' + currency);
}
});
// Обновляем цены в корзине
if (typeof tcart__updateDelivery === 'function') {
tcart__updateDelivery();
}
}
function extractNumber(text) {
const match = text.match(/\d+/g);
return match ? parseInt(match.join('')) : 0;
}
function extractRangeNumbers(text) {
const matches = text.match(/\d+/g);
if (matches && matches.length >= 2) {
return [parseInt(matches[0]), parseInt(matches[1])];
}
return [0, 0];
}
function formatNumber(num) {
return new Intl.NumberFormat('ru-RU').format(Math.round(num));
}
})();
</script>
<style>
div#delivery-services-wrapper label {
opacity: 0;
}
div#delivery-services-wrapper label.show-label {
opacity: 1;
}
span.delivery-minimum-price:after {
content: attr(data-text-price);
font-size: 15px;
}
span.delivery-minimum-price {
font-size: 0;
}
.uc-delivery-table{
display: none;
}
</style>