From e744b0a074b8e979e38f12981ce94e1871ccf8f3 Mon Sep 17 00:00:00 2001 From: w3bdesign <45217974+w3bdesign@users.noreply.github.com> Date: Mon, 6 Jul 2020 23:59:02 +0200 Subject: [PATCH] Fix price bug with variable products Fix price bug with variable products and replace for loop with a forEach --- components/Cart/Cart.component.jsx | 1 + components/Cart/CartPage/CartItem.component.jsx | 2 ++ .../Cart/CartPage/CartItemsContainer.component.jsx | 3 +++ utils/functions/functions.js | 13 +++++++------ 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/components/Cart/Cart.component.jsx b/components/Cart/Cart.component.jsx index 9c4cb2ea9..a287521af 100644 --- a/components/Cart/Cart.component.jsx +++ b/components/Cart/Cart.component.jsx @@ -11,6 +11,7 @@ import { AppContext } from 'utils/context/AppContext'; */ const Cart = () => { const [cart, setCart] = useContext(AppContext); + const productsCount = null !== cart && Object.keys(cart).length ? cart.totalProductsCount : ''; diff --git a/components/Cart/CartPage/CartItem.component.jsx b/components/Cart/CartPage/CartItem.component.jsx index ed0bdcbad..38c7391dd 100644 --- a/components/Cart/CartPage/CartItem.component.jsx +++ b/components/Cart/CartPage/CartItem.component.jsx @@ -25,10 +25,12 @@ const CartItem = ({ item, products, handleRemoveProductClick, updateCart }) => { }*/ // If the user tries to delete the count of product, set that to 1 by default ( This will not allow him to reduce it less than zero ) const newQty = event.target.value ? parseInt(event.target.value) : 1; + // Set the new quantity in state. setProductCount(newQty); if (products.length) { const updatedItems = getUpdatedItems(products, newQty, cartKey); + updateCart({ variables: { input: { diff --git a/components/Cart/CartPage/CartItemsContainer.component.jsx b/components/Cart/CartPage/CartItemsContainer.component.jsx index de25970b9..df0f29fc2 100644 --- a/components/Cart/CartPage/CartItemsContainer.component.jsx +++ b/components/Cart/CartPage/CartItemsContainer.component.jsx @@ -74,6 +74,9 @@ const CartItemsContainer = () => { const updatedCart = getFormattedCart(data); localStorage.setItem('woocommerce-cart', JSON.stringify(updatedCart)); // Update cart data in React Context. + + + setCart(updatedCart); }, onError: (error) => { diff --git a/utils/functions/functions.js b/utils/functions/functions.js index b1433d61f..10f65bc6d 100644 --- a/utils/functions/functions.js +++ b/utils/functions/functions.js @@ -198,16 +198,18 @@ export const getFormattedCart = (data) => { formattedCart.products = []; let totalProductsCount = 0; - for (let i = 0; i < givenProducts.length; i++) { + let i = 0; + givenProducts.forEach(() => { const givenProduct = givenProducts[i].product; const product = {}; - const total = getFloatVal(givenProducts[i].total); + // Convert price to a float value + const convertedCurrency = givenProducts[i].total.replace(/[^0-9.-]+/g, ''); product.productId = givenProduct.productId; product.cartKey = givenProducts[i].key; product.name = givenProduct.name; product.qty = givenProducts[i].quantity; - product.price = total / product.qty; + product.price = convertedCurrency / product.qty; product.totalPrice = givenProducts[i].total; product.image = { sourceUrl: givenProduct.image.sourceUrl, @@ -216,14 +218,13 @@ export const getFormattedCart = (data) => { }; totalProductsCount += givenProducts[i].quantity; - // Push each item into the products array. formattedCart.products.push(product); - } + i++; + }); formattedCart.totalProductsCount = totalProductsCount; formattedCart.totalProductsPrice = data.cart.total; - return formattedCart; };