function getInfoFromSelectedProduct(){ let ids = objProducts.map(obj => obj.id); return $.get($('meta[name="get-contents"]').attr('content')+"/"+ids.join(",")); } function numberToEuroFormat(number){ let formatter = new Intl.NumberFormat('de', {style: 'currency', currency: 'EUR'}); return formatter.format(number); } function getQuantityOfProducts(){ let quantitys = objProducts.map(obj => obj.quantity); let quantityOfProducts = 0; for(let i = 0; i < quantitys.length; i++) { if(Number.isInteger(Number(quantitys[i]))) quantityOfProducts += Number(quantitys[i]); } return quantityOfProducts; } function getProductInfo(productInfo){ let objProduct = objProducts.find(obj => obj.id == productInfo.id); return jQuery.extend(objProduct, productInfo); } function createProductElement(objProduct){ let elementProduct = ` ${objProduct.title}
sofort verfügbar
${numberToEuroFormat(objProduct.preis)} `; return elementProduct; } function addToBag(productId){ let objProductInfo = objProducts.find(obj => obj.id == productId); let index = objProducts.indexOf(objProductInfo); if(index == -1) objProducts.push({id: productId, quantity: 1}); updateProductsInLocalStorage(); } function isJson(str) { try { JSON.parse(str); } catch(e) { return false; } return true; } function isEncoded(str){ try { atob(str); } catch(e) { return false; } return true; } function hasCartCode(){ let urlPath = window.location.pathname; let code = urlPath.split("/"); code = code[code.length-1]; if(isEncoded(code)){ let productLink = atob(code); if(isJson(productLink)) return true; } return false; } function updateProductsInLocalStorage(){ localStorage.setItem("products", JSON.stringify(objProducts)); $('#count-products-in-bag').text(getQuantityOfProducts()); $('#list-products-in-bag').html(''); if(typeof updateProducts === "function") $('#list-products').html(''); totalPrice = 0; if(objProducts.length > 0){ getInfoFromSelectedProduct().done(function(response, status){ response.contents.forEach(function(product){ let objProduct = getProductInfo(product); totalPrice += objProduct.preis*objProduct.quantity; let elementProduct = createProductElement(objProduct); $('#list-products-in-bag').append(elementProduct); if(typeof updateProducts === "function") $('#list-products').append(elementProduct); }); $('#total-price').text(numberToEuroFormat(totalPrice)); if(typeof updateProducts === "function") $('#total-checkout-price').text(numberToEuroFormat(totalPrice)); }); $('.cart-link').show(); $(".btn-share").show(); $('#count-products-in-bag').show(); $('#list-products-in-bag').parent().parent().parent().parent().parent().find('.modal-footer').show(); $('#total-price').parent().parent().parent().show(); $('#no-products').hide(); if(typeof updateProducts === "function"){ $('#form-section').show(); $('#list-products').parent().parent().parent().parent().parent().find('.modal-footer').show(); $('#total-checkout-price').parent().parent().parent().show(); $('#no-products-in-checkout').hide(); } if(window.location.origin == 'http://localhost') cartLink = window.location.origin+'/areya-energy/public/'+btoa(JSON.stringify(objProducts)); else cartLink = window.location.origin+'/'+btoa(JSON.stringify(objProducts)); $('.cart-link').val(cartLink); shareData = { title: "Share cart list", text: 'share cart list', url: cartLink, }; }else{ $('.cart-link').hide(); $(".btn-share").hide(); $('#count-products-in-bag').hide(); $('#list-products-in-bag').parent().parent().parent().parent().parent().find('.modal-footer').hide(); $('#total-price').parent().parent().parent().hide(); $('#no-products').show(); if(typeof updateProducts === "function"){ $('#form-section').hide(); $('#list-products').parent().parent().parent().parent().parent().find('.modal-footer').hide(); $('#total-checkout-price').parent().parent().parent().hide(); $('#no-products-in-checkout').show(); } } let productId = $('meta[name="product-id"]').attr('content'); if(productId != undefined){ let objProduct = objProducts.find(obj => obj.id == productId); if(objProduct == undefined) { $('#btn-add-to-bag').show(); $('#btn-show-bag').hide(); if(objProducts.length > 0) $('#btn-check-out').parent().hide(); else $('#btn-check-out').parent().show(); } else { $('#btn-add-to-bag').hide(); $('#btn-show-bag').show(); $('#btn-check-out').parent().hide(); $('#count-items-in-bag').html(objProduct.quantity+``); } } }