From c5eed65d5bb8adcb994bbf20c40f3bc0026e8cf3 Mon Sep 17 00:00:00 2001 From: = <=> Date: Tue, 6 Sep 2022 03:21:51 -0500 Subject: [PATCH] calculate price in bag --- assets/js/methods.js | 29 +++++++++++++++++++++-------- assets/js/script.js | 22 ++++++++++++++++------ 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/assets/js/methods.js b/assets/js/methods.js index d4a8fed..29e7c59 100644 --- a/assets/js/methods.js +++ b/assets/js/methods.js @@ -20,7 +20,16 @@ function getProductInfo(productInfo){ return jQuery.extend(objProduct, productInfo); } +function calculatePrice(objProduct){ + objProduct.calc_preis = objProduct.preis; + if(objProduct.quantity >= 5) objProduct.calc_preis = objProduct.preis_5; + if(objProduct.quantity >= 10) objProduct.calc_preis = objProduct.preis_10; + if(Number(objProduct.quantity) >= Number(objProduct.module_pro_palette)) objProduct.calc_preis = objProduct.preis_palette; + return objProduct; +} + function createProductElement(objProduct){ + calculatePrice(objProduct); let elementProduct = ` @@ -33,16 +42,20 @@ function createProductElement(objProduct){
sofort verfügbar
- + - ${numberToEuroFormat(objProduct.preis)} + + ${numberToEuroFormat(objProduct.calc_preis)}`; + if(objProduct.calc_preis != objProduct.preis) elementProduct += ` +
+ + ${(((objProduct.calc_preis-objProduct.preis)*100)/objProduct.preis).toFixed(2)} % + `; + elementProduct += ` `; return elementProduct; } diff --git a/assets/js/script.js b/assets/js/script.js index 4a52cd0..c5b761a 100644 --- a/assets/js/script.js +++ b/assets/js/script.js @@ -80,25 +80,35 @@ $(window).on('load', function(){ } }); - $(document).on('change', '.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); let index = objProducts.indexOf(objProductInfo); objProducts[index].quantity = quantity; - localStorage.setItem("products", JSON.stringify(objProducts)); - + calculatePrice(objProductInfo); + let elementPrice = ` + ${numberToEuroFormat(objProductInfo.calc_preis)}`; + if(objProductInfo.calc_preis != objProductInfo.preis) elementPrice += ` +
+ + ${(((objProductInfo.calc_preis-objProductInfo.preis)*100)/objProductInfo.preis).toFixed(2)} % + `; + $(this).parent().parent().find('.product-price').html(elementPrice); $('#count-products-in-bag').text(getQuantityOfProducts()); getInfoFromSelectedProduct().done(function(response, status){ totalPrice = 0; response.contents.forEach(function(product){ let objProduct = getProductInfo(product); - totalPrice += objProduct.preis*objProduct.quantity; + calculatePrice(objProduct); + totalPrice += objProduct.calc_preis*objProduct.quantity; }); $('#total-price').text(numberToEuroFormat(totalPrice)); if(typeof updateProducts === "function") $('#total-checkout-price').text(numberToEuroFormat(totalPrice)); }); - productId = $('meta[name="product-id"]').attr('content'); if(productId != undefined){ let objProduct = objProducts.find(obj => obj.id == productId); @@ -115,7 +125,7 @@ $(window).on('load', function(){ $('#count-items-in-bag').html(objProduct.quantity+``); } } - + localStorage.setItem("products", JSON.stringify(objProducts)); }); $('#btn-copy-cart-link').click(function(){ $('.cart-link').select();