$(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);
});
});
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 main_product = deze.data('main_product');
var aantal = aantal;
var row_identifier = 0;
////// 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,'main_product':main_product,'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);
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 = new Array();
/*
for (i = 0; i < localStorage.length; i++) {
if(localStorage.key(i).indexOf('wl_shop_') == 0 ){
arraysort_var.push(JSON.parse(localStorage.getItem(localStorage.key(i))));
}
}
*/
Object.keys(localStorage).forEach(function(key){
if(key.indexOf('wl_shop_') > -1 ){
arraysort_var.push(JSON.parse(localStorage.getItem(key)));
}
});
arraysort_var.sort(dynamicSort("timestamp"));
var display_aantal = 0;
// alert(arraysort_var.length);
var aantal_items_in_localstorage = arraysort_var.length;
for (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;
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;
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;
}
totaal_row_prijs = (verkoop_prijs * 1) * (aantal * 1);
totaal_row_prijs = totaal_row_prijs.toMoney(2);
verkoop_prijs = (verkoop_prijs *1).toMoney(2);
/////get all wishes from localstorage
if (main_product == 1){
var arraysort_var_wish = new Array();
var data_wishes = '';
/*
for (j = 0; j < localStorage.length; j++) {
if(localStorage.key(j).indexOf('_'+timestamp) > -1 ){
arraysort_var_wish.push(JSON.parse(localStorage.getItem(localStorage.key(j))));
}
}
*/
Object.keys(localStorage).forEach(function(key){
if(key.indexOf(timestamp) > -1 ){
arraysort_var_wish.push(JSON.parse(localStorage.getItem(key)));
}
});
arraysort_var_wish.sort(dynamicSort("timestamp"));
var total_wish_prijs = 0;
for (j = 0; j < arraysort_var_wish.length; j++) {
var retrieved_array_wish = arraysort_var_wish[j];
var product_id_wish = retrieved_array_wish.product_id;
var product_wish = retrieved_array_wish.product;
var verkoop_prijs_wish = retrieved_array_wish.verkoop_prijs;
var company_id_wish = retrieved_array_wish.company_id;
var product_category_wish = retrieved_array_wish.product_category;
var main_product_wish = retrieved_array_wish.main_product;
var aantal_wish = retrieved_array_wish.aantal;
var row_identifier_wish = retrieved_array_wish.row_identifier;
var timestamp_wish = retrieved_array_wish.timestamp;
//alert('timestamp_wish: '+timestamp_wish+' product: '+product_wish);
verkoop_prijs_wish = verkoop_prijs_wish * 1;
total_wish_prijs = total_wish_prijs + verkoop_prijs_wish;
data_wishes += "
";
data_wishes += "
"+product_wish+"
";
if (verkoop_prijs_wish > 0){
data_wishes += "
(+SRD "+verkoop_prijs_wish.toMoney(2)+")
";
}
data_wishes += "
";
}
var prijs_plus_wish_prijs = (verkoop_prijs *1) + total_wish_prijs;
var totaal_row_prijs = (totaal_row_prijs * 1) + (total_wish_prijs * aantal);
data += "";
data += "
"+product+"
";
data += data_wishes;
data += "
";
if (delivery_fee_row == false){
data += "
-
";
data += "
";
data += "
+
";
}else{
data += "
";
}
data += "
SRD "+prijs_plus_wish_prijs.toMoney(2)+"
";
data += "
SRD "+totaal_row_prijs.toMoney(2)+"
";
if (delivery_fee_row == false){
data += "

";
}
data += "
";
//////
//////
//////
//////
/*
data_checkout += "";
data_checkout += "
"+product+"
";
data_checkout += data_wishes;
data_checkout += "
";
if (delivery_fee_row == false){
data_checkout += "
-
";
data_checkout += "
";
data_checkout += "
+
";
}else{
data_checkout += "
";
}
data_checkout += "
SRD "+prijs_plus_wish_prijs.toMoney(2)+"
";
data_checkout += "
SRD "+totaal_row_prijs.toMoney(2)+"
";
if (delivery_fee_row == false){
data_checkout += "

";
}
data_checkout += "
";
*/
data_checkout += "";
data_checkout += "
"+product+"
";
data_checkout += data_wishes;
data_checkout += "
";
data_checkout += "
";
data_checkout += "
SRD "+prijs_plus_wish_prijs.toMoney(2)+"
";
data_checkout += "
SRD "+totaal_row_prijs.toMoney(2)+"
";
data_checkout += "
";
//////
//////
//////
if (delivery_fee_row){
aantal = 0;
}
display_aantal = display_aantal + (aantal * 1);
}
}
// if (aantal_items_in_localstorage == 1){
// data = '';
// }
if (data == ''){
data += "Uw mandje is leeg
";
}
$('.winkelmand_wrap_inner').html('');
$('#bekijk_bestelling').html('');
iteration_test++
console.log('iteration: '+iteration_test);
//alert('empty data: '+data);
//$('#winkelmand_'+company_id).append(data);
$('.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;
});
$('#wl_shop_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 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_category':product_category,'main_product':main_product,'row_identifier':row_identifier,'timestamp':timestamp};
localStorage.setItem(deze_identifier, 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_verkooprijs = 0;
var arraysort_var_wish = new Array();
/*
for (j = 0; j < localStorage.length; j++) {
if(localStorage.key(j).indexOf('_'+timestamp) > -1 ){
arraysort_var_wish.push(JSON.parse(localStorage.getItem(localStorage.key(j))));
}
}*/
Object.keys(localStorage).forEach(function(key){
if(key.indexOf(timestamp) > -1 ){
//console.log(key);
//localStorage.removeItem(key);
arraysort_var_wish.push(JSON.parse(localStorage.getItem(key)));
}
});
arraysort_var_wish.sort(dynamicSort("timestamp"));
var total_wish_prijs = 0;
for (j = 0; j < arraysort_var_wish.length; j++) {
var retrieved_array_wish = arraysort_var_wish[j];
var product_id = retrieved_array_wish.product_id;
var product = retrieved_array_wish.product;
var verkoop_prijs = retrieved_array_wish.verkoop_prijs;
var company_id = retrieved_array_wish.company_id;
var product_category = retrieved_array_wish.product_category;
var main_product = retrieved_array_wish.main_product;
// var aantal = aantal;
var row_identifier = retrieved_array_wish.row_identifier;
// var timestamp = retrieved_array_wish.timestamp;
//alert('timestamp_wish: '+timestamp_wish+' product: '+product_wish);
var array = {'product_id':product_id,'product':product,'aantal':aantal,'verkoop_prijs':verkoop_prijs,'company_id':company_id,'product_category':product_category,'main_product':main_product,'row_identifier':row_identifier,'timestamp':timestamp};
localStorage.setItem('wl_shop_'+product_id+'_'+timestamp, JSON.stringify(array));
return_verkooprijs = return_verkooprijs + (verkoop_prijs * 1);
}
return(return_verkooprijs);
}
$(document).on('click','.delete_row','data',function(){
//clear_wl_shop_items_from_local_storage();
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 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,'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;
};