/**
* KAELUM Wix Integration Script v2.4
* Build: 2025.01.01.005
* Opens /Purchase directly (no /pay/wix redirect)
*/
(function () {
'use strict';
const siteKey = window.kaelumSiteKey;
const discount = Number(window.kaelumDiscountRate || 0.06);
if (!siteKey) {
console.error('[KAELUM-WIX] ❌ window.kaelumSiteKey is REQUIRED');
return;
}
console.log('[KAELUM-WIX] ✓ Script v2.4 loaded');
console.log('[KAELUM-WIX] Site:', siteKey.substring(0, 15) + '...');
console.log('[KAELUM-WIX] Discount:', (discount * 100).toFixed(1) + '%');
const BTN_ID = 'kaelum-pay-btn';
const WRAP_ID = 'kaelum-pay-wrap';
const LABEL = 'Pay with KAELUM — Save ' + (discount * 100).toFixed(0) + '%';
const LOGO_SVG = '';
const ATC_SELECTORS = [
'button[data-hook="add-to-cart"]',
'button[id*="add-to-cart" i]',
'button[class*="add-to-cart" i]',
'[data-hook="add-to-cart-button"]',
'button[aria-label*="Add to Cart" i]',
'button[aria-label*="Add to Bag" i]',
'form button[type="button"]',
'.product-page button[type="button"]'
];
function removeExisting() {
const existing = document.getElementById(WRAP_ID);
if (existing && existing.parentElement) {
existing.parentElement.removeChild(existing);
console.log('[KAELUM-WIX] 🗑️ Removed previous button');
}
}
function applyStyles(element, styles) {
for (const property in styles) {
element.style[property] = styles[property];
}
}
function createButton() {
const wrapper = document.createElement('div');
wrapper.id = WRAP_ID;
applyStyles(wrapper, {
width: '100%',
marginTop: '12px',
boxSizing: 'border-box'
});
const button = document.createElement('button');
button.id = BTN_ID;
button.type = 'button';
button.setAttribute('aria-label', 'Pay with KAELUM and save ' + (discount * 100).toFixed(0) + ' percent');
button.innerHTML = LOGO_SVG + '' + LABEL + '';
applyStyles(button, {
width: '100%',
display: 'inline-flex',
alignItems: 'center',
justifyContent: 'center',
gap: '0.5rem',
padding: '14px 16px',
margin: '0',
borderRadius: '10px',
border: 'none',
background: 'linear-gradient(90deg, rgb(255, 106, 58) 0%, rgb(255, 147, 77) 100%)',
boxShadow: '0 4px 12px rgba(255, 106, 58, 0.3)',
color: 'rgb(255, 255, 255)',
fontSize: '15px',
fontWeight: '700',
lineHeight: '1.2',
textAlign: 'center',
cursor: 'pointer',
transition: 'all 0.2s ease',
fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
boxSizing: 'border-box'
});
button.addEventListener('mouseenter', function() {
button.style.transform = 'translateY(-2px)';
button.style.boxShadow = '0 6px 16px rgba(255, 106, 58, 0.4)';
});
button.addEventListener('mouseleave', function() {
button.style.transform = 'translateY(0)';
button.style.boxShadow = '0 4px 12px rgba(255, 106, 58, 0.3)';
});
function updateResponsiveStyles() {
const isMobile = window.innerWidth <= 480;
button.style.fontSize = isMobile ? '14px' : '15px';
button.style.padding = isMobile ? '12px 14px' : '14px 16px';
}
updateResponsiveStyles();
window.addEventListener('resize', updateResponsiveStyles);
button.addEventListener('click', function(e) {
e.preventDefault();
e.stopPropagation();
console.log('[KAELUM-WIX] 🚀 Button clicked');
const priceEl = document.querySelector('[data-hook="formatted-primary-price"], .product-price, [data-hook="product-price"]');
const priceText = priceEl ? priceEl.textContent.trim() : '';
// Build Purchase URL directly (no /pay/wix redirect)
const url = new URL('https://kaelum.app/Purchase');
url.searchParams.set('site', siteKey);
url.searchParams.set('src', window.location.href);
url.searchParams.set('discount', String(discount * 100));
url.searchParams.set('v', '2025.01.01.005');
if (priceText) {
url.searchParams.set('priceText', priceText);
const match = priceText.match(/[£$€]?([0-9.,]+)/);
if (match) {
const price = match[1].replace(',', '');
url.searchParams.set('price', price);
console.log('[KAELUM-WIX] Detected price:', price);
}
}
console.log('[KAELUM-WIX] Opening popup:', url.toString());
const popup = window.open(
url.toString(),
'kaelum_checkout',
'width=500,height=700,menubar=no,toolbar=no,location=no,status=no'
);
if (!popup) {
console.error('[KAELUM-WIX] ❌ Popup blocked');
alert('Please allow popups for this site to use KAELUM checkout.');
} else {
console.log('[KAELUM-WIX] ✓ Popup opened successfully');
}
});
wrapper.appendChild(button);
return wrapper;
}
function tryInjectButton() {
console.log('[KAELUM-WIX] 🔍 Searching for Add to Cart button...');
for (let i = 0; i < ATC_SELECTORS.length; i++) {
const selector = ATC_SELECTORS[i];
const atcButton = document.querySelector(selector);
if (atcButton) {
console.log('[KAELUM-WIX] ✓ Found ATC with selector:', selector);
console.log('[KAELUM-WIX] ATC element:', atcButton);
removeExisting();
const kaelumButton = createButton();
atcButton.insertAdjacentElement('afterend', kaelumButton);
console.log('[KAELUM-WIX] ✅ Button injected after ATC');
console.log('[KAELUM-WIX] Button location:', kaelumButton.getBoundingClientRect());
return true;
}
}
console.log('[KAELUM-WIX] ⚠️ Add to Cart not found, trying again...');
return false;
}
let injectionAttempts = 0;
const maxAttempts = 20;
function attemptInjection() {
if (injectionAttempts >= maxAttempts) {
console.warn('[KAELUM-WIX] ⏱️ Max attempts reached, stopping search');
return;
}
injectionAttempts++;
if (!tryInjectButton()) {
setTimeout(attemptInjection, 250);
}
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', attemptInjection);
} else {
attemptInjection();
}
const observer = new MutationObserver(function(mutations) {
if (!document.getElementById(WRAP_ID)) {
console.log('[KAELUM-WIX] 🔄 DOM changed, re-checking button placement');
tryInjectButton();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
console.log('[KAELUM-WIX] ✓ Script fully initialized');
})();