DBP SIDING
BESTEL.INNODEEN.NL

kg

CO2 opslag 🌿

///// CO2 calculation ////// (async () => { // 1) Fetch cart data (lengths + profile-areas) async function fetchCartData() { try { const resp = await fetch('/winkelwagen'); const html = await resp.text(); const doc = new DOMParser().parseFromString(html, 'text/html'); let totalMm = 0, eind = 0, dil = 0, hoek = 0; doc.querySelectorAll('tr.woocommerce-cart-form__cart-item').forEach(item => { // straight length const lenTxt = item.querySelector('dd.variation-Lengtemm p')?.textContent || ''; const length = parseFloat(lenTxt.trim().replace(',', '.')) || 0; const qty = parseInt(item.querySelector('input.qty')?.value, 10) || 0; totalMm += length * qty; // profiles const type = item.querySelector('dd.variation-Type p')?.textContent.trim(); if (type === 'Eindprofiel') { eind += qty * ((3600 * 360.14) / 1e6); } else if (type === 'Dilatatieprofiel') { dil += qty * ((3600 * 282.22) / 1e6); } else if (type === 'Hoekprofiel') { hoek += qty * ((3600 * 504.86) / 1e6); } }); return { totalMm, profileAreaSum: eind + dil + hoek }; } catch { return { totalMm: 0, profileAreaSum: 0 }; } } // 2) Apply centering styles function applyCentering() { // wrapper and content: full-width, column flex, center children document.querySelectorAll('.elementor-image-box-wrapper, .elementor-image-box-content').forEach(el => { el.style.display = 'flex'; el.style.flexDirection = 'column'; el.style.alignItems = 'center'; el.style.width = '100%'; el.style.boxSizing = 'border-box'; el.style.overflow = 'visible'; }); // title: no wrap, centered text document.querySelectorAll('.elementor-image-box-title').forEach(h3 => { h3.style.display = 'inline-block'; h3.style.whiteSpace = 'nowrap'; h3.style.textAlign = 'center'; h3.style.margin = '0 auto'; h3.style.boxSizing = 'content-box'; h3.style.overflow = 'visible'; }); } // 3) Calculate & update, persist, then re-apply centering async function calculateAndUpdateValue() { const target = document.querySelector('.elementor-image-box-title'); if (!target) return; try { const { totalMm, profileAreaSum } = await fetchCartData(); const extraLen = parseFloat(document.getElementById('2126_lengte')?.value) || 0; const extraQty = parseInt(document.getElementById('quantity_686bde6e087c9')?.value, 10) || 0; const mm = totalMm + (extraLen * extraQty); const co2G = mm * 2.219 + profileAreaSum * 1000 * 2.219; const totalKg = co2G / 1000; const fmt = n => n.toFixed(3).replace('.', ','); const newText = `–${fmt(totalKg)} kg`; target.innerText = newText; localStorage.setItem('co2TotalText', newText); } catch (err) { console.error('CO₂ recalc failed:', err); } // ensure it's centered after we update applyCentering(); } // 4) Hook inputs + WooCommerce events (including removals) function initListeners() { ['2126_lengte', 'quantity_686bde6e087c9'].forEach(id => { const el = document.getElementById(id); if (el) el.addEventListener('input', calculateAndUpdateValue); }); if (window.jQuery) { jQuery('body').on( 'added_to_cart removed_from_cart wc_fragments_refreshed updated_cart_totals', calculateAndUpdateValue ); } document.body.addEventListener('click', e => { if (e.target.closest('a.remove')) setTimeout(calculateAndUpdateValue, 500); }); } // 5) On load: restore last value, run calc & centering document.addEventListener('DOMContentLoaded', () => { const titleEl = document.querySelector('.elementor-image-box-title'); const last = localStorage.getItem('co2TotalText'); if (titleEl && last) titleEl.innerText = last; initListeners(); calculateAndUpdateValue(); applyCentering(); }); })();

CHANNEL-155

€ 0,00

Type *
Kleur *
Kleur *

Categorie:
Scroll naar boven