document.addEventListener('DOMContentLoaded', function() {
const freeShippingAmount = 70; // GRATIS VERZENDING VANAF €70
const fill = document.getElementById("fs-progress-fill");
const left = document.getElementById("fs-left");
const textEl = document.querySelector('.fs-text');
function getCartTotal() {
// Probeer het totaal te lezen van de winkelwagenpagina
let totalEl = document.querySelector('.cart_totals .order-total .woocommerce-Price-amount bdi');
// Zo niet gevonden? probeer mini-cart
if (!totalEl) {
totalEl = document.querySelector('.widget_shopping_cart .total .woocommerce-Price-amount bdi');
}
if (!totalEl) return null;
let text = totalEl.innerText || totalEl.textContent;
text = text.replace(/[^\d,\,\.]/g, '');
text = text.replace('.', '').replace(',', '.');
const total = parseFloat(text);
return isNaN(total) ? null : total;
}
function updateBar() {
const total = getCartTotal();
if (total === null) return;
let remaining = freeShippingAmount - total;
let progress = (total / freeShippingAmount) * 100;
if (progress > 100) progress = 100;
fill.style.width = progress + '%';
// Tekst aanpassen onder/boven threshold
if (remaining > 0) {
// Nog niet gratis
left.innerHTML = remaining.toFixed(2).replace('.', ',');
textEl.innerHTML = `Nog €${left.innerHTML} tot gratis verzending`;
} else {
// Gratis verzending behaald
textEl.innerHTML = `Je bestelling wordt gratis verzonden.
More budget for good hair days.`;
}
}
updateBar();
jQuery(document.body).on(
'wc_fragments_refreshed wc_cart_button_updated added_to_cart removed_from_cart updated_cart_totals',
updateBar
);
});