$(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', '#ad_upsell_to_winkelmand', function() {
console.log('Add to cart clicked');
var deze = $(this);
var d = new Date();
var timestamp = d.getTime();
var prev_element = $('#aantal');
var aantal = 1;
if (prev_element.hasClass('aantal')) {
aantal = prev_element.val() * 1;
}
console.log('Main product:', deze.data('product-id'));
console.log('Aantal:', aantal);
add_product_to_winkelmand(deze, aantal, timestamp);
$('.product_wrapper_upsell_active').each(function() {
var dit = $(this);
console.log('Upsell product:', dit.data('product-id'));
add_product_to_winkelmand(dit, aantal, timestamp);
});
});
function add_product_to_winkelmand(deze, aantal, timestamp){
var product_id = deze.data('product_id');
var product = deze.data('product');
var verkoop_prijs = deze.data('verkoop_prijs');
var company_id = deze.data('company_id');
var product_category = deze.data('product_category');
var product_image = deze.data('product_image');
var main_product = deze.data('main_product');
var channel = deze.data('channel');
var aantal = aantal;
var row_identifier = 0;
if (channel === 'upsell') {
console.log('UPSSELL DETECTED');
console.log('Product ID:', product_id);
console.log('Product:', product);
console.log('Prijs:', verkoop_prijs);
console.log('Timestamp:', timestamp);
}
////// later toevoegen
//////'artikel_identifier':artikel_id_identifier,'opmerking':deze_opmerking,'entryid':deze_entry_id,'minimale_afname':deze_minimale_afname,
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':row_identifier,'timestamp':timestamp};
var count_items = 0;
/////toegevoegd voor delivery fee//////
/////toegevoegd voor delivery fee//////
if (timestamp == 9999999999999){
//alert('delivery');
product_id = 'deliveryfee';
}
/////toegevoegd voor delivery fee//////
/////toegevoegd voor delivery fee//////
check_if_localstorage_key_exists(product_id, array, count_items, timestamp, main_product, channel);
var speed = 1300;
pulse_widget(speed);
$(".winkelmand_wrap_inner").scrollTop($(".winkelmand_wrap_inner")[0].scrollHeight);
}
function check_if_localstorage_key_exists(product_id, array, count_items, timestamp, main_product){
//var iteration = 1;
var temp_product_id = product_id;
if (count_items){
temp_product_id = product_id+'_'+count_items;
}
if (main_product == 0){
//alert('product_ID: '+product_id);
temp_product_id = temp_product_id+'_'+timestamp;
}
if (localStorage.hasOwnProperty('wl_shop_'+temp_product_id) && timestamp != 9999999999999) {
count_items++
check_if_localstorage_key_exists(product_id, array, count_items, timestamp, main_product);
}else{
if (count_items || main_product == 0){
product_id = temp_product_id;
}
array.row_identifier = count_items;
localStorage.setItem('wl_shop_'+product_id, JSON.stringify(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.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;
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 * 1;
$('.row_prijs_totaal:visible').each(function() {
var dit = $(this);
var waarde = dit.html() * 1;
//console.log(waarde);
totaal_prijs = totaal_prijs + waarde;
});
$('.total_price').html(totaal_prijs.toMoney(2));
$('#wl_shop_total_price_mobile').html(totaal_prijs.toMoney(2));
$('#checkout_totaal').html(totaal_prijs.toMoney(2));
}
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');
deze_id_stripped = deze_id.substring(11);
$('#row_wl_shop_'+deze_id_stripped).slideUp(800);
$('#row_wl_shop_'+deze_id_stripped).find('.product_blok_winkelmand_inner').fadeOut(250);
//}, 100);
var retrieved_array = JSON.parse(localStorage.getItem('wl_shop_'+deze_id_stripped));
var timestamp = retrieved_array.timestamp;
delete_subproducts(timestamp);
localStorage.removeItem('wl_shop_'+deze_id_stripped);
// update_winkelmand_icoon();
window.setTimeout(function(){
get_items_from_local_storage()
}, 1000);
}
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();
});
function update_winkelmand_prijzen(){
var productIDs = new Object();
var val = '';
for (i = 0; i < localStorage.length; i++) {
//console.log(localStorage.key(i) + "=[" + localStorage.getItem(localStorage.key(i)) + "]");
val = localStorage.key(i);
//if(localStorage.key(i).startsWith("wl_shop_") ){
if(localStorage.key(i).indexOf('wl_shop_') == 0 ){
var retrieved_array = JSON.parse(localStorage.getItem(localStorage.key(i)));
productIDs[val] = retrieved_array.product_id;
}
}
$.ajax({
url: 'companies_producten.php',
type: 'GET',
data: {class:'entry',task:'wl_shop_get_prijzen_for_IDs',attributes:productIDs},
cache: false,
// dataType:'json',
success: function (html) {
//alert(html);
var rows = html.split('|');
var prijzen_aangepast = false;
var producten_uit_voorraad = '';
for (var key in rows) {
var detail = rows[key].split('=');
//alert(detail[1]);
console.log(localStorage);
var retrieved_array = JSON.parse(localStorage.getItem(detail[0]));
if (!retrieved_array) continue;
var old_prijs = retrieved_array.verkoop_prijs * 1;
var product_id = retrieved_array.product_id;
var product = retrieved_array.product;
var verkoop_prijs = detail[1] * 1;;
var company_id = retrieved_array.company_id;
var product_category = retrieved_array.product_category;
var product_image = retrieved_array.product_image;
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= '+product_aantal);
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,'row_identifier':row_identifier,'timestamp':timestamp};
if (verkoop_prijs != 0){
localStorage.setItem(detail[0], JSON.stringify(array));
if (old_prijs != verkoop_prijs){
prijzen_aangepast = true;
}
}
if (detail[2] == 0){
producten_uit_voorraad += ""+product+"
";
localStorage.removeItem(detail[0]);
}
// localStorage.detail[0][3] = 200;
}
if (producten_uit_voorraad != ''){
producten_uit_voorraad = "De volgende producten zijn helaas niet in voorraad:
"+producten_uit_voorraad+"
Deze producten zijn verwijderd uit uw winkelmand
";
if (prijzen_aangepast){
producten_uit_voorraad = producten_uit_voorraad+"
De prijs van sommmige producten uit uw mandje zijn geüpdatet!
";
}
wl_alert('alert',producten_uit_voorraad,false,false,'Ok','',false);
}else if(prijzen_aangepast){
wl_alert('alert','De prijs van sommmige producten uit uw mandje zijn geüpdatet!',false,false,'Ok','',false);
}
get_items_from_local_storage();
},
error: function(error){
wl_alert('alert','Connection lost, please try again.' + error,false,false,'Ok','',false);
console.log('error'+error);
},
complete: function(){
$('.loading').hide();
}
});
}
$(window).on("blur focus", function(e) {
var prevType = $(this).data("prevType");
if (prevType != e.type) { // reduce double fire issues
switch (e.type) {
case "blur":
// do work
break;
case "focus":
update_winkelmand_prijzen();
break;
}
}
$(this).data("prevType", e.type);
});
Number.prototype.toMoney = function(decPlaces) {
if (decPlaces == null){
decPlaces = 0
}
var result = Math.round(parseFloat((this * Math.pow(10, decPlaces)).toFixed(13))) / Math.pow(10, decPlaces);
result = result.toFixed(decPlaces);
return result;
};