<script>
(function () {
'use strict';
const partUidsExceptions = ['508492596383'];
const DEBUG = false;
const WATCH_INTERVAL = 200;
const MAX_WATCH_TIME = 30000;
function log() {
if (!DEBUG) return;
console.log('[CustomPromo]', ...arguments);
}
function calculateDiscountWithExceptions(totalAmount, products, promo) {
if (!promo) {
return null;
}
const productsList = products || window.tcart?.products || [];
let exceptionsSum = 0;
productsList.forEach(function (product) {
if (!product) return;
if (product.deleted === 'yes') return;
if (typeof product === 'object' && Object.keys(product).length === 0) {
return;
}
const partUids = Array.isArray(product.part_uids) ? product.part_uids : [];
const isException = partUidsExceptions.some(function (exceptionUid) {
return partUids.includes(exceptionUid);
});
if (isException) {
exceptionsSum += Number(product.amount) || 0;
}
});
if (exceptionsSum <= 0) {
return null;
}
const discountableAmount = Math.max(0, Number(totalAmount) - exceptionsSum);
if (Number(promo.discountpercent) > 0) {
const percent = Number(promo.discountpercent);
const discount = tcart_ceil(discountableAmount * percent / 100);
const total = tcart_ceil(Number(totalAmount) - discount);
return {
discount: discount,
total: total
};
}
if (Number(promo.discountsum) > 0) {
const requestedDiscount = Number(promo.discountsum);
const discount = Math.min(requestedDiscount, discountableAmount);
const total = tcart_ceil(Number(totalAmount) - discount);
return {
discount: discount,
total: total
};
}
return null;
}
function applyCustomResult(cartData, result) {
if (!cartData || !result) {
return;
}
cartData.prodamount_discountsum = result.discount;
cartData.prodamount_withdiscount = result.total;
if (cartData.promocode) {
cartData.promocode.prodamount_discountsum = result.discount;
}
if (window.tcart) {
window.tcart.prodamount_discountsum = result.discount;
window.tcart.prodamount_withdiscount = result.total;
if (window.tcart.promocode) {
window.tcart.promocode.prodamount_discountsum = result.discount;
}
}
updateDisplay(result.discount, result.total);
}
function updateDisplay(discount, total) {
try {
const discountElements = document.querySelectorAll('.t-inputpromocode__wrapper .t706__cartwin-prodamount-price');
if (discountElements.length >= 2) {
const element = discountElements[discountElements.length - 2];
if (element) {
element.innerHTML = typeof tcart__showPrice === 'function' ? tcart__showPrice(discount, 'acceptzero') : discount;
}
}
const totalElements = document.querySelectorAll('.t706__cartwin-totalamount');
totalElements.forEach(function (element) {
if (typeof tcart__showPrice === 'function') {
element.innerHTML = tcart__showPrice(total, 'acceptzero');
} else {
element.innerHTML = total;
}
});
} catch (error) {
log('Display update error:', error);
}
}
function createPromoWrapper(originalFn) {
if (typeof originalFn !== 'function') {
return null;
}
if (originalFn.__customPromoWrapped) {
return originalFn;
}
function wrappedCalcPromocode(totalAmount, cartData) {
log('_calcPromocode called', { totalAmount: totalAmount, promocode: cartData?.promocode });
const promo = cartData?.promocode;
if (promo && (Number(promo.discountsum) > 0 || Number(promo.discountpercent) > 0)) {
const result = calculateDiscountWithExceptions(totalAmount, cartData.products, promo);
if (result) {
log('CUSTOM CALCULATION', result);
applyCustomResult(cartData, result);
return result.total;
}
}
return originalFn.call(this, totalAmount, cartData);
}
wrappedCalcPromocode.__customPromoWrapped = true;
wrappedCalcPromocode._intercepted = true;
wrappedCalcPromocode.__customPromoOriginal = originalFn;
return wrappedCalcPromocode;
}
function interceptPrototype() {
if (!window.CartCalculator || !window.CartCalculator.prototype) {
return false;
}
const proto = window.CartCalculator.prototype;
if (typeof proto._calcPromocode !== 'function') {
return false;
}
if (proto._calcPromocode.__customPromoWrapped) {
return true;
}
const wrapped = createPromoWrapper(proto._calcPromocode);
if (!wrapped) {
return false;
}
proto._calcPromocode = wrapped;
log('Prototype intercepted');
return true;
}
function interceptExistingInstance() {
if (!window.cartCalculator) {
return false;
}
if (typeof window.cartCalculator._calcPromocode !== 'function') {
return false;
}
if (window.cartCalculator._calcPromocode.__customPromoWrapped) {
return true;
}
const wrapped = createPromoWrapper(window.cartCalculator._calcPromocode);
if (!wrapped) {
return false;
}
window.cartCalculator._calcPromocode = wrapped;
log('Existing instance intercepted');
return true;
}
function interceptConstructor() {
if (typeof window.CartCalculator !== 'function') {
return false;
}
if (window.CartCalculator.__customPromoConstructorWrapped) {
return true;
}
const OriginalCartCalculator = window.CartCalculator;
function CustomCartCalculator() {
const instance = new OriginalCartCalculator(...arguments);
if (instance && typeof instance._calcPromocode === 'function' && !instance._calcPromocode.__customPromoWrapped) {
instance._calcPromocode = createPromoWrapper(instance._calcPromocode);
}
log('New CartCalculator intercepted');
return instance;
}
CustomCartCalculator.prototype = OriginalCartCalculator.prototype;
Object.assign(CustomCartCalculator, OriginalCartCalculator);
CustomCartCalculator.__customPromoConstructorWrapped = true;
CustomCartCalculator.__customPromoOriginal = OriginalCartCalculator;
window.CartCalculator = CustomCartCalculator;
log('Constructor intercepted');
return true;
}
function install() {
const hasCalculator = typeof window.CartCalculator === 'function';
if (!hasCalculator) {
log('Waiting for CartCalculator...');
return false;
}
const prototypeInstalled = interceptPrototype();
const existingInstanceInstalled = interceptExistingInstance();
const constructorInstalled = interceptConstructor();
const prototypeStillInstalled = interceptPrototype();
const success = prototypeInstalled || existingInstanceInstalled || prototypeStillInstalled;
if (success) {
log('INSTALL SUCCESS', {
prototypeInstalled: prototypeInstalled,
existingInstanceInstalled: existingInstanceInstalled,
constructorInstalled: constructorInstalled
});
}
return success;
}
function forceCartUpdate() {
try {
if (typeof window.tcart__updateTotalProductsinCartObj === 'function') {
window.tcart__updateTotalProductsinCartObj();
return;
}
if (window.cartCalculator && window.tcart && typeof window.cartCalculator.updateProductsData === 'function') {
window.cartCalculator.updateProductsData(window.tcart);
}
} catch (error) {
log('Force update error:', error);
}
}
const startTime = Date.now();
let installedOnce = false;
let updateTriggered = false;
function watcher() {
const installed = install();
if (installed) {
installedOnce = true;
if (!updateTriggered) {
updateTriggered = true;
setTimeout(forceCartUpdate, 300);
}
}
if (Date.now() - startTime < MAX_WATCH_TIME) {
setTimeout(watcher, WATCH_INTERVAL);
} else {
log('Watcher finished', { installedOnce: installedOnce });
}
}
document.addEventListener('DOMContentLoaded', function () {
log('DOMContentLoaded');
install();
});
window.addEventListener('load', function () {
log('WINDOW LOAD');
install();
setTimeout(install, 500);
setTimeout(install, 1500);
setTimeout(install, 3000);
});
if (typeof MutationObserver !== 'undefined') {
const observer = new MutationObserver(function () {
if (!installedOnce || !window.cartCalculator) {
install();
}
});
observer.observe(document.documentElement, {
childList: true,
subtree: true
});
setTimeout(function () {
observer.disconnect();
}, MAX_WATCH_TIME);
}
log('Custom promo script started');
watcher();
window.__customPromoDebug = {
status: function () {
return {
CartCalculator: typeof window.CartCalculator,
cartCalculator: !!window.cartCalculator,
prototypeIntercepted: !!(window.CartCalculator && window.CartCalculator.prototype && window.CartCalculator.prototype._calcPromocode && window.CartCalculator.prototype._calcPromocode.__customPromoWrapped),
instanceIntercepted: !!(window.cartCalculator && window.cartCalculator._calcPromocode && window.cartCalculator._calcPromocode.__customPromoWrapped),
constructorIntercepted: !!(window.CartCalculator && window.CartCalculator.__customPromoConstructorWrapped)
};
},
recalc: function () {
forceCartUpdate();
},
products: function () {
return (window.tcart?.products || []).map(function (product) {
return {
title: product.title || product.name,
amount: product.amount,
part_uids: product.part_uids
};
});
}
};
})();
</script>