Empreinte
-- Faire un choix -- Empreinte: Texte +
100.00
Dhs HT
document.addEventListener('DOMContentLoaded', function () {
const priceWrapper = document.querySelector('.custom-price-wrapper');
const amountElement = document.querySelector('.custom-amount');
if (!priceWrapper || !amountElement) {
console.warn("⚠️ Éléments de prix introuvables !");
return;
}
const basePrice = parseFloat(priceWrapper.getAttribute('data-base-price')) || 0;
function formatPrice(price) {
return price.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ' ');
}
function parseOptionPrice(option) {
if (!option || !option.value) return 0;
let price = 0;
const label = option.textContent.toLowerCase();
const priceAttr = option.getAttribute('price');
if (priceAttr !== null && priceAttr !== '') {
const parsed = parseFloat(priceAttr);
if (!isNaN(parsed)) return parsed;
}
if (label.includes('+')) {
const match = label.match(/\+\s*(\d+[.,]?\d*)/);
if (match) {
price = parseFloat(match[1].replace(',', '.'));
}
}
return isNaN(price) ? 0 : price;
}
function updateTotalPrice() {
let total = basePrice;
const selects = document.querySelectorAll('select.product-custom-option, select.super-attribute-select, select[data-selector*="super_attribute"], select[name*="options"]');
selects.forEach(select => {
const selected = select.options[select.selectedIndex];
total += parseOptionPrice(selected);
});
const formatted = formatPrice(total);
amountElement.textContent = formatted;
priceWrapper.setAttribute('data-current-price', total);
}
let savedSelections = {};
function saveSelections() {
savedSelections = {};
document.querySelectorAll('select.product-custom-option').forEach(select => {
savedSelections[select.name] = select.value;
});
}
function restoreSelections() {
document.querySelectorAll('select.product-custom-option').forEach(select => {
if (savedSelections[select.name]) {
select.value = savedSelections[select.name];
select.dispatchEvent(new Event('change'));
}
});
}
const allSelects = document.querySelectorAll('select.product-custom-option, select.super-attribute-select, select[data-selector*="super_attribute"], select[name*="options"]');
allSelects.forEach(select => {
select.addEventListener('change', function () {
if (this.classList.contains('super-attribute-select')) {
saveSelections();
setTimeout(() => {
restoreSelections();
updateTotalPrice();
}, 100);
} else {
updateTotalPrice();
}
});
});
updateTotalPrice();
});
Empreinte: Texte et logo +
160.00
Dhs HT
document.addEventListener('DOMContentLoaded', function () {
const priceWrapper = document.querySelector('.custom-price-wrapper');
const amountElement = document.querySelector('.custom-amount');
if (!priceWrapper || !amountElement) {
console.warn("⚠️ Éléments de prix introuvables !");
return;
}
const basePrice = parseFloat(priceWrapper.getAttribute('data-base-price')) || 0;
function formatPrice(price) {
return price.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ' ');
}
function parseOptionPrice(option) {
if (!option || !option.value) return 0;
let price = 0;
const label = option.textContent.toLowerCase();
const priceAttr = option.getAttribute('price');
if (priceAttr !== null && priceAttr !== '') {
const parsed = parseFloat(priceAttr);
if (!isNaN(parsed)) return parsed;
}
if (label.includes('+')) {
const match = label.match(/\+\s*(\d+[.,]?\d*)/);
if (match) {
price = parseFloat(match[1].replace(',', '.'));
}
}
return isNaN(price) ? 0 : price;
}
function updateTotalPrice() {
let total = basePrice;
const selects = document.querySelectorAll('select.product-custom-option, select.super-attribute-select, select[data-selector*="super_attribute"], select[name*="options"]');
selects.forEach(select => {
const selected = select.options[select.selectedIndex];
total += parseOptionPrice(selected);
});
const formatted = formatPrice(total);
amountElement.textContent = formatted;
priceWrapper.setAttribute('data-current-price', total);
}
let savedSelections = {};
function saveSelections() {
savedSelections = {};
document.querySelectorAll('select.product-custom-option').forEach(select => {
savedSelections[select.name] = select.value;
});
}
function restoreSelections() {
document.querySelectorAll('select.product-custom-option').forEach(select => {
if (savedSelections[select.name]) {
select.value = savedSelections[select.name];
select.dispatchEvent(new Event('change'));
}
});
}
const allSelects = document.querySelectorAll('select.product-custom-option, select.super-attribute-select, select[data-selector*="super_attribute"], select[name*="options"]');
allSelects.forEach(select => {
select.addEventListener('change', function () {
if (this.classList.contains('super-attribute-select')) {
saveSelections();
setTimeout(() => {
restoreSelections();
updateTotalPrice();
}, 100);
} else {
updateTotalPrice();
}
});
});
updateTotalPrice();
});
Sans empreinte