From acb1503e8de835bbc3db07a64193b2c8b8dab3ce Mon Sep 17 00:00:00 2001 From: Gustavo Luigi <=> Date: Thu, 8 Sep 2022 12:13:43 +0200 Subject: [PATCH] add try catch in load products --- assets/js/checkout.js | 30 ++++++++++++++++++++++++------ assets/js/methods.js | 11 +++++++++++ content/master.blade.php | 3 +++ content/pages/checkout.blade.php | 3 +++ 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/assets/js/checkout.js b/assets/js/checkout.js index 249c57d..29bae62 100644 --- a/assets/js/checkout.js +++ b/assets/js/checkout.js @@ -35,16 +35,32 @@ function updateProducts(){ $('#list-products').append(elementProduct); }); $('#total-checkout-price').text(numberToEuroFormat(totalPrice)); + }).fail(function(response){ + $('#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').hide(); + $('#cart-error-in-checkout').show(); }); $('#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(); - }else{ + $('#cart-error-in-checkout').hide(); + } + else if(totalPrice > 0){ + $('#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').hide(); + $('#cart-error-in-checkout').show(); + } + else{ $('#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(); + $('#cart-error-in-checkout').hide(); } } @@ -60,12 +76,14 @@ $(window).on('load', function(){ let products = response.contents.map(product => { product = getProductInfo(product); product = calculatePrice(product); - return product; + return product.quantity+'x '+product.title+' ('+numberToEuroFormat(product.calc_preis)+')'; }); - $('#ccart').val(JSON.stringify({"total-price": totalPrice, "products": products})); - objProducts = []; - localStorage.setItem("products", JSON.stringify(objProducts)); - form.unbind('submit').submit(); + let cart = products.join('\n')+"\nTotal Price: "+numberToEuroFormat(totalPrice); + $('#ccart').val(cart); + console.log($('form').serializeArray()); + //objProducts = []; + //localStorage.setItem("products", JSON.stringify(objProducts)); + //form.unbind('submit').submit(); }); }); $(document).on('click', '.btn-remove-product', function(){ diff --git a/assets/js/methods.js b/assets/js/methods.js index 30b495d..0a7450d 100644 --- a/assets/js/methods.js +++ b/assets/js/methods.js @@ -113,6 +113,15 @@ function updateProductsInLocalStorage(){ $('#list-products-in-bag').append(elementProduct); }); $('#total-price').text(numberToEuroFormat(totalPrice)); + }).fail(function(response){ + $('.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').hide(); + $('#cart-error').show(); + $('#modal-buttons').hide(); }); $('.cart-link').show(); $(".btn-share").show(); @@ -120,6 +129,7 @@ function updateProductsInLocalStorage(){ $('#list-products-in-bag').parent().parent().parent().parent().parent().find('.modal-footer').show(); $('#total-price').parent().parent().parent().show(); $('#no-products').hide(); + $('#cart-error').hide(); $('#modal-buttons').show(); cartLink = window.location.origin+'/'+btoa(JSON.stringify(objProducts)); @@ -139,6 +149,7 @@ function updateProductsInLocalStorage(){ $('#list-products-in-bag').parent().parent().parent().parent().parent().find('.modal-footer').hide(); $('#total-price').parent().parent().parent().hide(); $('#no-products').show(); + $('#cart-error').hide(); $('#modal-buttons').hide(); } let productId = $('meta[name="product-id"]').attr('content'); diff --git a/content/master.blade.php b/content/master.blade.php index 173481c..23ba643 100644 --- a/content/master.blade.php +++ b/content/master.blade.php @@ -106,6 +106,9 @@