From 3d42690169fb05362461fd62c6e11146b862f1c9 Mon Sep 17 00:00:00 2001 From: Gustavo Luigi <=> Date: Mon, 21 Nov 2022 16:25:06 -0300 Subject: [PATCH] bugfix in cart --- assets/js/script.js | 69 +++++++++++++++++++++------------------- content/master.blade.php | 1 + 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/assets/js/script.js b/assets/js/script.js index 12031b0..618e3b9 100644 --- a/assets/js/script.js +++ b/assets/js/script.js @@ -1,29 +1,32 @@ -$(window).on('load', function(){ - if(localStorage.getItem("dismiss-message") == 1) $('.alert-dismissible').remove(); +$(window).on('load', function () { + $.ajaxSetup({ + headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }, + }); + if (localStorage.getItem("dismiss-message") == 1) $('.alert-dismissible').remove(); else $('.alert-dismissible').show(); - $('#btn-close-message').click(function(){ localStorage.setItem("dismiss-message", 1); }); - if(localStorage.getItem("products") != null) objProducts = JSON.parse(localStorage.getItem("products")); + $('#btn-close-message').click(function () { localStorage.setItem("dismiss-message", 1); }); + if (localStorage.getItem("products") != null) objProducts = JSON.parse(localStorage.getItem("products")); updateProductsInLocalStorage(); - if(hasCartCode()){ + if (hasCartCode()) { let productLink = getCartCode(); - if(objProducts.length > 0) $('#modal-confirm-product-replacement').modal('show'); + if (objProducts.length > 0) $('#modal-confirm-product-replacement').modal('show'); else $('#modal-confirm-addition-of-products').modal('show'); - $('.btn-confirm-product-link').click(function(){ + $('.btn-confirm-product-link').click(function () { objProducts = JSON.parse(productLink); updateProductsInLocalStorage(); window.location.replace($('meta[name="checkout"]').attr('content')); }); } - $(document).on('click', '.btn-remove-product', function(){ + $(document).on('click', '.btn-remove-product', function () { let productId = $(this).attr('data-id'); let objProductInfo = objProducts.find(obj => obj.id == productId); let index = objProducts.indexOf(objProductInfo); - if(index != -1) objProducts.splice(index, 1); + if (index != -1) objProducts.splice(index, 1); localStorage.setItem("products", JSON.stringify(objProducts)); $(this).parent().parent().remove(); $('#count-products-in-bag').text(getQuantityOfProducts()); - if(objProducts.length > 0){ + if (objProducts.length > 0) { $('.cart-link').show(); $(".btn-share").show(); $('#count-products-in-bag').show(); @@ -31,7 +34,7 @@ $(window).on('load', function(){ $('#total-price').parent().parent().parent().show(); $('#no-products').hide(); $('#modal-buttons').show(); - }else{ + } else { $('.cart-link').hide(); $(".btn-share").hide(); $('#count-products-in-bag').hide(); @@ -41,35 +44,35 @@ $(window).on('load', function(){ $('#modal-buttons').hide(); } - getInfoFromSelectedProduct().done(function(response, status){ + getInfoFromSelectedProduct().done(function (response, status) { totalPrice = 0; - if(response.contents != undefined) response.contents.forEach(function(product){ + if (response.contents != undefined) response.contents.forEach(function (product) { let objProduct = getProductInfo(product); - totalPrice += objProduct.preis*objProduct.quantity; + totalPrice += objProduct.preis * objProduct.quantity; }); $('#total-price').text(numberToEuroFormat(totalPrice)); - if(typeof updateProducts === "function") $('#total-checkout-price').text(numberToEuroFormat(totalPrice)); + if (typeof updateProducts === "function") $('#total-checkout-price').text(numberToEuroFormat(totalPrice)); }); productId = $('meta[name="product-id"]').attr('content'); - if(productId != undefined){ + if (productId != undefined) { let objProduct = objProducts.find(obj => obj.id == productId); - if(objProduct == undefined) { + if (objProduct == undefined) { $('#btn-add-to-bag').show(); $('#btn-show-bag').hide(); - if(objProducts.length > 0) $('#btn-check-out').parent().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+``); + $('#count-items-in-bag').html(objProduct.quantity + ``); } } }); - $(document).on('input', '.select-quantity', function(){ + $(document).on('input', '.select-quantity', function () { let quantity = $(this).val(); let productId = $(this).attr('data-id'); let objProductInfo = objProducts.find(obj => obj.id == productId); @@ -77,56 +80,56 @@ $(window).on('load', function(){ objProducts[index].quantity = quantity; localStorage.setItem("products", JSON.stringify(objProducts)); let productPrice = $(this).parent().parent().find('.product-price'); - $.get($('meta[name="get-contents"]').attr('content')+"/"+objProductInfo.id).done(function(response, status){ + $.get($('meta[name="get-contents"]').attr('content') + "/" + objProductInfo.id).done(function (response, status) { let product = response.contents[0]; let objProduct = getProductInfo(product); objProduct = calculatePrice(objProduct); let elementPrice = ` ${numberToEuroFormat(objProduct.calc_preis)}`; - if(objProduct.calc_preis != objProduct.preis) elementPrice += ` + if (objProduct.calc_preis != objProduct.preis) elementPrice += `
- ${(((objProduct.calc_preis-objProduct.preis)*100)/objProduct.preis).toFixed(2)} % + ${(((objProduct.calc_preis - objProduct.preis) * 100) / objProduct.preis).toFixed(2)} % `; productPrice.html(elementPrice); }); $('#count-products-in-bag').text(getQuantityOfProducts()); - getInfoFromSelectedProduct().done(function(response, status){ + getInfoFromSelectedProduct().done(function (response, status) { totalPrice = 0; - response.contents.forEach(function(product){ + response.contents.forEach(function (product) { let objProduct = getProductInfo(product); calculatePrice(objProduct); - totalPrice += objProduct.calc_preis*objProduct.quantity; + totalPrice += objProduct.calc_preis * objProduct.quantity; }); $('#total-price').text(numberToEuroFormat(totalPrice)); }); productId = $('meta[name="product-id"]').attr('content'); - if(productId != undefined){ + if (productId != undefined) { let objProduct = objProducts.find(obj => obj.id == productId); - if(objProduct == undefined) { + if (objProduct == undefined) { $('#btn-add-to-bag').show(); $('#btn-show-bag').hide(); - if(objProducts.length > 0) $('#btn-check-out').parent().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+``); + $('#count-items-in-bag').html(objProduct.quantity + ``); } } }); - $('#btn-copy-cart-link').click(function(){ + $('#btn-copy-cart-link').click(function () { $('.cart-link').select(); document.execCommand("copy"); $(this).html(``); }); - $(".btn-share").click(async function() { - if(navigator.share) { try { await navigator.share(shareData); } catch(err) { console.warn(err); } } + $(".btn-share").click(async function () { + if (navigator.share) { try { await navigator.share(shareData); } catch (err) { console.warn(err); } } else { console.warn('Native Web Sharing not supported'); } }); }); \ No newline at end of file diff --git a/content/master.blade.php b/content/master.blade.php index 17d6df7..6678911 100644 --- a/content/master.blade.php +++ b/content/master.blade.php @@ -1,6 +1,7 @@ +