<script>
document.addEventListener("DOMContentLoaded", function() {
setTimeout(function() {
t_onReady(function() {
setTimeout(function() {
t_onFuncLoad('tcart__init', function() {
setTimeout(initDeliveryPaymentMapping, 1500);
});
}, 200);
});
}, 0);
});
function initDeliveryPaymentMapping() {
const deliveryPaymentMap = {
"Курьером до квартиры": ["Яндекс.Деньгами"]
};
const deliveryWrapper = document.querySelector('.t-radio__wrapper-delivery');
const paymentLabels = document.querySelectorAll('.t-radio__wrapper-payment label');
if (!deliveryWrapper || !paymentLabels.length) return;
const paymentTextToIndex = new Map();
paymentLabels.forEach((label, index) => {
const paymentText = label.textContent.trim();
paymentTextToIndex.set(paymentText, index);
});
function resetPaymentMethods() {
paymentLabels.forEach(label => {
label.classList.remove('hide-payment');
const radio = label.querySelector('input');
if (radio) radio.disabled = false;
});
}
function applyDeliveryRules(deliveryText) {
if (!deliveryText) return;
resetPaymentMethods();
let availablePayments = null;
for (const [deliveryKey, payments] of Object.entries(deliveryPaymentMap)) {
if (deliveryText.includes(deliveryKey)) {
availablePayments = payments;
break;
}
}
if (!availablePayments) return;
const paymentsToHide = [];
paymentLabels.forEach((label) => {
const paymentText = label.textContent.trim();
const isAvailable = availablePayments.some(available =>
paymentText.includes(available)
);
if (!isAvailable) {
paymentsToHide.push(label);
}
});
paymentsToHide.forEach(label => {
label.classList.add('hide-payment');
const radio = label.querySelector('input');
if (radio) radio.disabled = true;
});
if (availablePayments.length > 0) {
let availableSelected = false;
paymentLabels.forEach(label => {
const radio = label.querySelector('input');
if (radio && radio.checked && !radio.disabled) {
availableSelected = true;
}
});
if (!availableSelected) {
for (const label of paymentLabels) {
const paymentText = label.textContent.trim();
if (availablePayments.some(available => paymentText.includes(available))) {
const radio = label.querySelector('input');
if (radio && !radio.disabled) {
radio.checked = true;
radio.dispatchEvent(new Event('change', { bubbles: true }));
break;
}
}
}
}
}
}
function getCurrentDelivery() {
if (deliveryWrapper.id === "customdelivery") {
return document.querySelector('input[name="tildadelivery-type"]:checked')?.value;
} else {
return document.querySelector('input.t-radio_delivery:checked')?.value;
}
}
function initializeOnLoad() {
setTimeout(function() {
const currentDelivery = getCurrentDelivery();
if (currentDelivery) {
applyDeliveryRules(currentDelivery);
} else {
setTimeout(function() {
const retryDelivery = getCurrentDelivery();
if (retryDelivery) {
applyDeliveryRules(retryDelivery);
}
}, 500);
}
}, 300);
}
if (deliveryWrapper.id === "customdelivery") {
let observer = new MutationObserver(function() {
setTimeout(function() {
const checkedDelivery = document.querySelector('input[name="tildadelivery-type"]:checked');
if (checkedDelivery) {
applyDeliveryRules(checkedDelivery.value);
}
}, 300);
});
observer.observe(deliveryWrapper, { childList: true, subtree: true });
initializeOnLoad();
} else {
deliveryWrapper.addEventListener('change', function(event) {
if (event.target.matches('input.t-radio_delivery')) {
setTimeout(function() {
const checkedDelivery = document.querySelector('input.t-radio_delivery:checked');
if (checkedDelivery) {
applyDeliveryRules(checkedDelivery.value);
}
}, 100);
}
});
initializeOnLoad();
}
}
</script>
<style>
.t-radio__wrapper-payment label.hide-payment{
position: absolute;
z-index: -1;
opacity: 0;
}
</style>