<script>
document.addEventListener("DOMContentLoaded", function() {
(function () {
const qrLink = 'AXPBelcP4P';
//Название скидки в системе
const dnDelivery = 'Скидка по QR';
//Подпись для подсказки в корзине
const discountHint = 'Скидка по акции 40%';
const hours = 1; //Время сохранения в часах
let discount = undefined;
t_onReady(function() {
setTimeout(function() {
t_onFuncLoad('tcart__init',function() {
let discountSteps = 0;
const discountAwait = setInterval(function() {
if ( window.t_cart__discounts !== void 0 ){
clearInterval(discountAwait);
discount = t_cart__discounts;
controlDiscounts();
};
if(discountSteps>20) clearInterval(discountAwait);
discountSteps++;
}, 100);
});
},200);
});
function controlDiscounts(){
const url = new URL(window.location.href);
const params = new URLSearchParams(url.search);
const savedTime = localStorage.getItem('qractive');
if (savedTime) {
const savedDate = new Date(savedTime);
const currentDate = new Date();
const timeDifference = currentDate - savedDate;
if (timeDifference > (hours * 60 * 60 * 1000)) {
localStorage.removeItem('qractive');
checkQrLink();
} else {
activateDiscountMode();
}
} else {
checkQrLink();
};
function checkQrLink(){
if(params.has(qrLink)) {
localStorage.setItem('qractive', new Date().toISOString());
params.delete(qrLink);
const newSearch = params.toString();
const newUrl = url.origin + url.pathname + (newSearch ? '?' + newSearch : '') + url.hash;
window.history.replaceState({}, '', newUrl);
activateDiscountMode();
}else{
deActivateDiscountMode();
};
};
};
function activateDiscountMode(){
if (!discount || !Array.isArray(discount)) {
console.warn('Массив скидок ещё не доступен. Поле промокода не тронуто.');
return;
}
const hasDnDelivery = discount.some(item => item.name === dnDelivery);
if (hasDnDelivery) {
window.t_cart__discounts = discount.filter(item => item.name === dnDelivery);
const originalTcartDict = window.tcart_dict;
window.tcart_dict = function(key) {
var text = originalTcartDict(key);
if (key === 'discount') return discountHint;
return text;
};
document.querySelector('.t706 .t-input-group_pc')?.remove();
document.querySelector('.t706').classList.add('discount-mode');
} else {
console.warn(`Скрипт: Скидка "${dnDelivery}" не найдена в корзине. Поле промокода оставлено активным.`);
}
};
function deActivateDiscountMode(){
const originalLength = discount.length;
const filteredDiscounts = discount.filter(item => item.name !== dnDelivery);
const newLength = filteredDiscounts.length;
if (originalLength !== newLength) {
window.t_cart__discounts = filteredDiscounts;
};
};
})();
});
</script>
<style>
.discount-mode ul.t706__cartwin-discounts__description {
font-size: 0;
}
.discount-mode ul.t706__cartwin-discounts__description:before {
content: "Специальная скидка 40% по QR коду";
font-size: 14px;
}
</style>