$(document).on('click','#voeg_toe_winkelmand','data',function(){
//clear_wl_shop_items_from_local_storage();
var deze = $(this);
var d=new Date();
var timestamp = d.getTime();
var prev_element = $('#aantal');
var aantal = 1;
if (prev_element.hasClass('aantal')){
var aantal = prev_element.val() * 1;
}
add_product_to_winkelmand(deze, aantal, timestamp);
$('.product_wrapper_wish_active').each(function() {
var dit = $(this);
add_product_to_winkelmand(dit, aantal, timestamp);
});
});
$(document).on('click', '#voeg_toe_winkelmand_upsell', function(e) {
e.preventDefault();
var timestamp = Date.now();
var $upsells = $('.upsell_product_wrapper.product_wrapper_upsell_active');
console.log('[UPSELL CART] Geselecteerd:', $upsells.length);
if (!$upsells.length) {
console.log('Geen upsells geselecteerd');
return;
}
$upsells.each(function() {
var $item = $(this);
var pid = $item.attr('data-product-id') ||
$item.attr('data-product_id');
if (!pid) {
console.error('Product ID ontbreekt', $item);
return;
}
var $qty = $('#aantal_upsell_product_' + pid);
var aantal = $qty.length
? parseInt($qty.val(), 10)
: 1;
if (!Number.isFinite(aantal) || aantal < 1) {
aantal = 1;
}
// Verplichte eigenschappen
$item.attr('data-channel', 'upsell');
$item.attr('data-main_product', '0');
// jQuery data-cache bijwerken
$item.data('channel', 'upsell');
$item.data('main_product', 0);
console.log('[UPSELL CART] Toevoegen:', {
product_id: pid,
aantal: aantal,
timestamp: timestamp
});
add_product_to_winkelmand($item, aantal, timestamp);
});
console.log('[UPSELL CART] Verwerking voltooid');
});
function add_product_to_winkelmand(deze, aantal, timestamp) {
var product_id = deze.attr('data-product_id') ||
deze.attr('data-product-id');
var product = deze.data('product');
var verkoop_prijs = Number(deze.data('verkoop_prijs')) || 0;
var company_id = deze.data('company_id');
var product_category = deze.data('product_category');
var product_image = deze.data('product_image');
var channel = deze.data('channel') || 'main-product';
var main_product = channel === 'upsell'
? 0
: Number(deze.data('main_product')) || 0;
aantal = parseInt(aantal, 10) || 1;
if (!product_id || !product) {
console.error('Productgegevens ontbreken:', {
product_id: product_id,
product: product,
element: deze
});
return;
}
var array = {
product_id: product_id,
product: product,
aantal: aantal,
verkoop_prijs: verkoop_prijs,
company_id: company_id,
product_category: product_category,
product_image: product_image,
main_product: main_product,
channel: channel,
row_identifier: 0,
timestamp: timestamp
};
check_if_localstorage_key_exists(
product_id,
array,
0,
timestamp,
main_product,
channel
);
console.log('[LOCALSTORAGE] Product toegevoegd:', array);
pulse_widget(1300);
var $cart = $('.winkelmand_wrap_inner');
if ($cart.length) {
$cart.scrollTop($cart[0].scrollHeight);
}
}
$(document).on('click','#checkout_overlay, #checkout_wijzig_bestelling, .close_checkout_box','data',function(e){
$('.product_wrapper_upsell_active')
.removeClass('product_wrapper_upsell_active');
// ✅ verberg ALLE aantal-secties
$("[id^='aantal_section_upsell_product_']").hide();
hide_checkout_box();
});
$(document).on('click','#checkout_overlay, #checkout_wijzig_bestelling, .close_checkout_box','data',function(e){
$('.product_wrapper_upsell_active').removeClass('product_wrapper_upsell_active');
});
function check_if_localstorage_key_exists(
product_id,
array,
count_items,
timestamp,
main_product,
channel
) {
channel = channel || 'main-product';
var temp_product_id = product_id;
// Bestaande hoofdproduct-sleutel behouden
if (main_product == 0) {
temp_product_id = product_id + '_' + timestamp;
}
// Upsells apart identificeren
if (channel === 'upsell') {
temp_product_id = 'upsell_' + temp_product_id;
}
if (count_items > 0) {
temp_product_id += '_' + count_items;
}
var storage_key = 'wl_shop_' + temp_product_id;
if (
localStorage.getItem(storage_key) !== null &&
timestamp != 9999999999999
) {
check_if_localstorage_key_exists(
product_id,
array,
count_items + 1,
timestamp,
main_product,
channel
);
return;
}
array.row_identifier = count_items;
localStorage.setItem(storage_key, JSON.stringify(array));
console.log('[STORAGE SAVED]', storage_key, array);
get_items_from_local_storage();
}
function dynamicSort(property) {
var sortOrder = 1;
if(property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
return function (a,b) {
var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
return result * sortOrder;
}
}
iteration_test = 0;
function get_items_from_local_storage() {
var data = '';
var data_checkout = '';
var arraysort_var = [];
Object.keys(localStorage).forEach(function(key) {
if (key.indexOf('wl_shop_') === 0) {
var item = JSON.parse(localStorage.getItem(key));
if (
item.main_product == 1 ||
item.channel === 'upsell' ||
item.product_category == 'Delivery Fee'
) {
arraysort_var.push(item);
}
}
});
arraysort_var.sort(dynamicSort("timestamp"));
var display_aantal = 0;
for (var i = 0; i < arraysort_var.length; i++) {
var retrieved_array = arraysort_var[i];
var product_id = retrieved_array.product_id;
var product = retrieved_array.product;
var verkoop_prijs = retrieved_array.verkoop_prijs * 1;
var product_image = retrieved_array.product_image;
var product_category = retrieved_array.product_category;
var main_product = retrieved_array.main_product;
var aantal = retrieved_array.aantal * 1;
var row_identifier = retrieved_array.row_identifier;
var timestamp = retrieved_array.timestamp;
if (row_identifier) {
row_identifier = product_id + "_" + row_identifier;
} else {
row_identifier = product_id;
}
var delivery_fee_row = false;
if (product_category == 'Delivery Fee') {
row_identifier = 'deliveryfee';
delivery_fee_row = true;
}
var data_wishes = '';
var data_upsells = '';
var total_wish_prijs = 0;
var total_upsell_prijs = 0;
console.log('===== WINKELMAND DEBUG =====');
Object.keys(localStorage)
.filter(function(key) {
return key.indexOf('wl_shop_') === 0;
})
.forEach(function(key) {
var item = JSON.parse(localStorage.getItem(key));
console.log({
storage_key: key,
product_id: item.product_id,
product: item.product,
channel: item.channel,
main_product: item.main_product,
aantal: item.aantal,
verkoop_prijs: item.verkoop_prijs,
timestamp: item.timestamp
});
});
console.log('===== EINDE DEBUG =====');
if (main_product == 1) {
var arraysort_var_wish = [];
var arraysort_var_upsell = [];
Object.keys(localStorage).forEach(function(key) {
if (key.indexOf('_' + timestamp) > -1) {
var item = JSON.parse(localStorage.getItem(key));
if (item.main_product == 0) {
if (item.channel === 'upsell') {
arraysort_var_upsell.push(item);
} else {
arraysort_var_wish.push(item);
}
}
}
});
arraysort_var_wish.sort(dynamicSort("timestamp"));
arraysort_var_upsell.sort(dynamicSort("timestamp"));
for (var j = 0; j < arraysort_var_wish.length; j++) {
var wish = arraysort_var_wish[j];
var prijs_wish = wish.verkoop_prijs * 1;
total_wish_prijs += prijs_wish;
data_wishes += "
";
data_wishes += "
" + wish.product + "
";
if (prijs_wish > 0) {
data_wishes += "
(+SRD " + prijs_wish.toMoney(2) + ")
";
}
data_wishes += "
";
}
for (var k = 0; k < arraysort_var_upsell.length; k++) {
var upsell = arraysort_var_upsell[k];
var prijs_upsell = upsell.verkoop_prijs * 1;
total_upsell_prijs += prijs_upsell;
data_upsells += "";
data_upsells += "
" + upsell.product + "
";
if (prijs_upsell > 0) {
data_upsells += "
(+SRD " + prijs_upsell.toMoney(2) + ")
";
}
data_upsells += "
";
}
}
var prijs_plus_subproducten = verkoop_prijs + total_wish_prijs + total_upsell_prijs;
var totaal_row_prijs = prijs_plus_subproducten * aantal;
data += "";
data += "
";
data += "
";
data += "
";
data += "

";
data += "
";
data += "
";
data += "
";
data += "
" + product + "
";
data += data_wishes;
data += data_upsells;
data += "
";
data += "
SRD " + totaal_row_prijs.toMoney(2) + "
";
data += "
";
data += "
";
if (delivery_fee_row == false) {
data += "
";
data += "
-
";
data += "
";
data += "
+
";
data += "
";
} else {
data += "
";
}
if (delivery_fee_row == false) {
data += "
Remove";
}
data += "
";
data += "
";
data += "
";
data += "
";
data_checkout += "";
data_checkout += "
";
data_checkout += "
" + product + "
";
data_checkout += data_wishes;
data_checkout += data_upsells;
data_checkout += "
";
data_checkout += "
";
data_checkout += "
SRD " + prijs_plus_subproducten.toMoney(2) + "
";
data_checkout += "
";
data_checkout += "
";
data_checkout += "
SRD " + totaal_row_prijs.toMoney(2) + "
";
data_checkout += "
";
data_checkout += "
";
data_checkout += "
";
if (delivery_fee_row) {
aantal = 0;
}
display_aantal += aantal;
}
if (data == '') {
data += "Uw mandje is leeg
";
}
$('.winkelmand_wrap_inner').html(data);
$('#bekijk_bestelling').html(data_checkout);
calculate_totaal_prijs();
calculate_winkelmand_aantal(display_aantal);
}
$(document).ready(function() {
get_items_from_local_storage();
});
function calculate_totaal_prijs() {
var totaal_prijs = 0;
// Alleen producten uit de hoofdwinkelmand berekenen
$('#winkelmand_1 .row_prijs_totaal').each(function() {
var prijs = parseFloat($(this).text()) || 0;
totaal_prijs += prijs;
});
// Totaal bijwerken
$('.total_price').html(totaal_prijs.toMoney(2));
$('#wl_shop_total_price_mobile').html(totaal_prijs.toMoney(2));
$('#checkout_totaal').html(totaal_prijs.toMoney(2));
console.log('[WINKELMAND] Correct totaal:', totaal_prijs);
}
var prevent_double_pulse = false;
function pulse_widget(speed){
if (prevent_double_pulse == false){
prevent_double_pulse = true;
var el1 = $('.bestelling_plaatsen_mobile');
var el2 = $('.bestelling_plaatsen');
el1.css('transition','none');
el2.css('transition','none');
window.setTimeout(function(){
el1.velocity("callout.bounce");
el2.velocity("callout.bounce");
window.setTimeout(function(){
el1.css('transition','0.3s ease');
el2.css('transition','0.3s ease');
}, 1300);
window.setTimeout(function(){
prevent_double_pulse = false;
}, 5000);
}, speed);
}
}
function calculate_winkelmand_aantal(display_aantal){
// Element zonder extra class
var winkelmand_aantal_html = "" + display_aantal + "
";
// Element met extra unieke class voor de button
var winkelmand_button_html = "" + display_aantal + "
";
$('.wl-shop_winkelmand_aantal').remove();
$('.winkelmand_image_inner').append(winkelmand_aantal_html);
$('.winkelmand_button').append(winkelmand_button_html);
}
$(document).on('input','.wl_numbers_only','',function(){
var deze = $(this);
var deze_waarde = deze.val();
deze_waarde = deze_waarde.replace(/[^0-9\.\,]/g,'');
if (deze_waarde.indexOf(',') > -1){
deze_waarde = deze_waarde.replace(/,/g, '.');
}
deze.val(deze_waarde);
});
$(document).on('change','.wl_shop_aantal_table','',function(){
var deze = $(this);
var deze_waarde = deze.val();
if (deze_waarde < 1){
deze.val(1);
deze.trigger('input');
}
var speed = 100;
pulse_widget(speed);
});
$(document).on('input','.wl_shop_aantal_table','',function(){
var deze = $(this);
var deze_identifier = deze.data('localstorage_id');
//deze_identifier = deze_identifier.substring(12);
//deze_identifier = "wl_shop_"+deze_identifier;
//alert(deze_identifier);
var deze_id = deze.attr('id');
var deze_product_prijs = deze.data('product_prijs');
var deze_waarde = deze.val();
var retrieved_array = JSON.parse(localStorage.getItem(deze_identifier));
var product_id = retrieved_array.product_id;
var product = retrieved_array.product;
var verkoop_prijs = retrieved_array.verkoop_prijs;
var product_image = retrieved_array.product_image;
var company_id = retrieved_array.company_id;
var product_category = retrieved_array.product_category;
var main_product = retrieved_array.main_product;
var aantal = retrieved_array.aantal;
var row_identifier = retrieved_array.row_identifier;
var timestamp = retrieved_array.timestamp;
// alert('aantal= '+aantal);
aantal = deze_waarde * 1;
var array = {'product_id':product_id,'product':product,'aantal':aantal,'verkoop_prijs':verkoop_prijs,'company_id':company_id,'product_image':product_image, 'product_category':product_category,'main_product':main_product,'row_identifier':row_identifier,'timestamp':timestamp};
var unique_key = 'wl_shop_' + product_id + '_' + timestamp;
array.row_identifier = product_id + '_' + timestamp;
localStorage.setItem(unique_key, JSON.stringify(array));
var deze_row = deze.closest('.product_blok_winkelmand');
var deze_target = deze_row.find('.row_prijs_totaal');
var totaal_sub_products = update_subproducts(timestamp, aantal);
var totaal_prijs_row = (aantal * ((verkoop_prijs *1) + (totaal_sub_products *1))).toMoney(2);
deze_target.html(totaal_prijs_row);
calculate_totaal_prijs();
var display_aantal = 0;
var winkelmand_wrap = $('.winkelmand_wrap_inner');
var target_wl_shop_aantal = winkelmand_wrap.find('.wl_shop_aantal_table:not("#wl_shop_identity_deliveryfee")');
target_wl_shop_aantal.each(function() {
var dit = $(this);
var waarde = dit.val() * 1;
//console.log(waarde);
display_aantal = display_aantal + waarde;
});
calculate_winkelmand_aantal(display_aantal);
});
function update_subproducts(timestamp, aantal) {
var return_verkoopprijs = 0;
Object.keys(localStorage).forEach(function(key) {
if (key.indexOf('_' + timestamp) > -1) {
var item = JSON.parse(localStorage.getItem(key));
if (item.main_product == 0) {
item.aantal = aantal;
localStorage.setItem(key, JSON.stringify(item));
return_verkoopprijs += item.verkoop_prijs * 1;
}
}
});
return return_verkoopprijs;
}
$(document).on('click','.delete_row','data',function(){
var deze = $(this);
wl_shop_ask_to_delete(deze);
});
function wl_shop_ask_to_delete(deze){
wl_alert('confirm', 'Wilt u dit item verwijderen uit uw mandje?',wl_shop_delete_row,false,'ja','nee',deze);
}
function wl_shop_delete_row(deze) {
var deze_id = deze.attr('id');
var productID = deze_id.replace('delete_row_', '');
console.log('[DELETE] Product ID:', productID);
// Zoek de exacte storage-key van dit product.
var storageKey = deze.attr('data-storage-key');
if (!storageKey) {
var matches = Object.keys(localStorage).filter(function(key) {
if (!key.startsWith('wl_shop_')) {
return false;
}
try {
var item = JSON.parse(localStorage.getItem(key));
return item &&
String(item.product_id) === String(productID);
} catch (error) {
return false;
}
});
if (matches.length !== 1) {
console.warn(
'[DELETE] Geen unieke storage-key gevonden:',
productID,
matches
);
return;
}
storageKey = matches[0];
}
var retrieved_array;
try {
retrieved_array = JSON.parse(localStorage.getItem(storageKey));
} catch (error) {
console.error('[DELETE] Ongeldige storage-data:', error);
return;
}
if (!retrieved_array) {
console.warn('[DELETE] Product niet gevonden:', storageKey);
return;
}
console.log('[DELETE] Storage-key:', storageKey);
console.log('[DELETE] Product:', retrieved_array.product);
// Subproducten alleen verwijderen bij een hoofdproduct.
if (
Number(retrieved_array.main_product) === 1 &&
retrieved_array.channel !== 'upsell' &&
retrieved_array.timestamp != null
) {
delete_subproducts(retrieved_array.timestamp);
}
// Verwijder het exacte product uit localStorage.
localStorage.removeItem(storageKey);
console.log('[DELETE] Verwijderd:', storageKey);
// Winkelmand opnieuw opbouwen.
get_items_from_local_storage();
}
function delete_subproducts(timestamp){
//alert(localStorage.length);
Object.keys(localStorage).forEach(function(key){
//if(key.startsWith("wl_shop_") ){
if(key.indexOf(timestamp) > -1 ){
//console.log(key);
localStorage.removeItem(key);
}
// get_items_from_local_storage();
//console.log(localStorage.getItem(key));
});
/*
window.setTimeout(function(){
for (var j = 0; j < (localStorage.length * 2); j++) {
//alert(localStorage.key(j));
if(localStorage.key(j).indexOf(timestamp) > -1 ){
localStorage.removeItem(localStorage.key(j));
// arraysort_var_wish.push(JSON.parse(localStorage.getItem(localStorage.key(j))));
}
}
}, 1000);
*/
}
$(document).on('click','.login_button','data',function(){
//clear_wl_shop_items_from_local_storage();
});
function clear_wl_shop_items_from_local_storage(){
//loop door alle Localstorage items heen
Object.keys(localStorage).forEach(function(key){
//if(key.startsWith("wl_shop_") ){
if(key.indexOf('wl_shop_') == 0 ){
//console.log(key);
localStorage.removeItem(key);
}
get_items_from_local_storage();
//console.log(localStorage.getItem(key));
});
}
function get_product_info_from_db_to_local_storage(productID){
var func_attributes = new Array();
func_attributes[0]=productID;
// func_attributes[1]=bedrag;
// func_attributes[2]=change;
$.ajax({
url: "process_localstorage.php",
type: "POST",
data: {class:'product',task:'get_product_info_for_local_storage',attributes:func_attributes},
// cache: false,
// async:false,
// dataType:'json',
success: function (html) {
$('#producten_bx_inner').html(html);
var deze = $('#delivery_fee');
var d=new Date('2100-12-12');
var timestamp = d.getTime();
console.log(timestamp);
timestamp = 9999999999999;
var prev_element = $('#aantal');
var aantal = 1;
add_product_to_winkelmand(deze, aantal, timestamp);
}
});
}
$(document).on('click','.update_prijzen','data',function(){
update_winkelmand_prijzen();
});
var winkelmandPrijsUpdateBezig = false;
function update_winkelmand_prijzen() {
// Voorkom meerdere gelijktijdige AJAX-requests
if (winkelmandPrijsUpdateBezig) {
console.log('[PRIJS UPDATE] Update is al bezig');
return;
}
var productIDs = {};
// Producten ophalen uit localStorage
Object.keys(localStorage).forEach(function(key) {
if (key.indexOf('wl_shop_') !== 0) {
return;
}
try {
var item = JSON.parse(localStorage.getItem(key));
if (item && item.product_id) {
productIDs[key] = item.product_id;
}
} catch (error) {
console.error('[PRIJS UPDATE] Ongeldig product:', key, error);
}
});
if (Object.keys(productIDs).length === 0) {
console.log('[PRIJS UPDATE] Geen producten gevonden');
get_items_from_local_storage();
return;
}
winkelmandPrijsUpdateBezig = true;
console.log('[PRIJS UPDATE] Start:', productIDs);
$.ajax({
url: 'companies_producten.php',
type: 'GET',
data: {
class: 'entry',
task: 'wl_shop_get_prijzen_for_IDs',
attributes: productIDs
},
cache: false,
success: function(html) {
var rows = String(html).split('|');
var prijzen_aangepast = false;
var producten_uit_voorraad = '';
rows.forEach(function(row) {
if (!row || row.indexOf('=') === -1) {
return;
}
var detail = row.split('=');
var storageKey = detail[0];
if (!storageKey || storageKey.indexOf('wl_shop_') !== 0) {
return;
}
var stored = localStorage.getItem(storageKey);
if (!stored) {
return;
}
var retrieved_array;
try {
retrieved_array = JSON.parse(stored);
} catch (error) {
console.error('[PRIJS UPDATE] JSON error:', storageKey);
return;
}
if (!retrieved_array) {
return;
}
var product = retrieved_array.product;
var old_prijs = Number(retrieved_array.verkoop_prijs) || 0;
var nieuwe_prijs = Number(detail[1]);
// Ongeldige prijs niet opslaan
if (
detail[1] === undefined ||
String(detail[1]).trim() === '' ||
!Number.isFinite(nieuwe_prijs) ||
nieuwe_prijs < 0
) {
console.warn(
'[PRIJS UPDATE] Ongeldige prijs:',
storageKey,
detail[1]
);
return;
}
// Channel behouden of herstellen
var channel = retrieved_array.channel;
if (!channel) {
if (storageKey.indexOf('wl_shop_upsell_') === 0) {
channel = 'upsell';
} else {
channel = 'main-product';
}
}
// Bestaand object behouden!
// Zo verliezen we geen productgegevens.
retrieved_array.verkoop_prijs = nieuwe_prijs;
retrieved_array.channel = channel;
// Controleer voorraadstatus
var voorraadStatus = detail[2];
if (String(voorraadStatus).trim() === '0') {
producten_uit_voorraad += '' + product + '
';
localStorage.removeItem(storageKey);
console.log(
'[PRIJS UPDATE] Niet op voorraad:',
product
);
return;
}
// Ook gratis producten (prijs 0) opslaan
localStorage.setItem(
storageKey,
JSON.stringify(retrieved_array)
);
if (old_prijs !== nieuwe_prijs) {
prijzen_aangepast = true;
}
console.log('[PRIJS UPDATE] Product bijgewerkt:', {
product: product,
channel: channel,
oude_prijs: old_prijs,
nieuwe_prijs: nieuwe_prijs
});
});
// Meldingen
if (producten_uit_voorraad !== '') {
var melding =
'De volgende producten zijn helaas niet in voorraad:
' +
producten_uit_voorraad +
'
' +
'Deze producten zijn verwijderd uit uw winkelmand' +
'
';
if (prijzen_aangepast) {
melding +=
'
' +
'De prijs van sommige producten is bijgewerkt!' +
'
';
}
wl_alert(
'alert',
melding,
false,
false,
'Ok',
'',
false
);
} else if (prijzen_aangepast) {
wl_alert(
'alert',
'De prijs van sommige producten in uw mandje is bijgewerkt!',
false,
false,
'Ok',
'',
false
);
}
// Winkelmand opnieuw opbouwen
get_items_from_local_storage();
console.log('[PRIJS UPDATE] Voltooid');
},
error: function(xhr, status, error) {
console.error('[PRIJS UPDATE] AJAX error:', {
status: status,
error: error,
response: xhr.responseText
});
wl_alert(
'alert',
'Connection lost, please try again.',
false,
false,
'Ok',
'',
false
);
},
complete: function() {
winkelmandPrijsUpdateBezig = false;
$('.loading').hide();
console.log('[PRIJS UPDATE] Request afgerond');
}
});
}
// Browser focus detecteren
$(document).ready(function() {
var windowHasFocus = document.hasFocus();
$(window).on('blur', function() {
windowHasFocus = false;
});
$(window).on('focus', function() {
if (windowHasFocus) {
return;
}
windowHasFocus = true;
console.log('[WINDOW] Focus terug - prijzen controleren');
update_winkelmand_prijzen();
});
});
// Prijs formatteren
Number.prototype.toMoney = function(decPlaces) {
if (decPlaces == null) {
decPlaces = 0;
}
var number = Number(this);
if (!Number.isFinite(number)) {
number = 0;
}
var factor = Math.pow(10, decPlaces);
return (
Math.round((number + Number.EPSILON) * factor) / factor
).toFixed(decPlaces);
};