MICROCURRENT THERAPIE (X 5)
<div id="voucher-box">
<label for="voucher">Kortingscode</label>
<input id="voucher" type="text" placeholder="Voer je code in">
<button id="applyVoucher">Toepassen</button>
<div id="voucherMsg"></div>
</div>
<script>
const orderTotalEl = document.getElementById('orderTotal'); // jouw element met totaal
let currentTotal = parseFloat(orderTotalEl?.dataset.total || '0');
document.getElementById('applyVoucher').addEventListener('click', async () => {
const code = document.getElementById('voucher').value.trim();
const res = await fetch('http://localhost:3001/api/vouchers/validate', {
method: 'POST',
headers: {'Content-Type':'application/json'},
body: JSON.stringify({
code,
orderTotal: currentTotal,
userId: window.USER_ID || null,
isFirstPurchase: window.IS_FIRST_PURCHASE || false
})
});
const data = await res.json();
const msg = document.getElementById('voucherMsg');
if (!data.ok) {
msg.textContent = 'Code ongeldig: ' + (data.error || 'onbekende fout');
return;
}
msg.textContent = `Korting toegepast: -€${data.discount.toFixed(2)} → nieuw totaal €${data.newTotal.toFixed(2)}`;
currentTotal = data.newTotal;
// update je UI hier (totaalbedrag, overzicht, etc.)
});
</script>