Skip to content

Commit d017798

Browse files
authored
Merge pull request #199 from w3bdesign/development
Fix price bug with variable products
2 parents 709688b + e744b0a commit d017798

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

components/Cart/Cart.component.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { AppContext } from 'utils/context/AppContext';
1111
*/
1212
const Cart = () => {
1313
const [cart, setCart] = useContext(AppContext);
14+
1415

1516
const productsCount =
1617
null !== cart && Object.keys(cart).length ? cart.totalProductsCount : '';

components/Cart/CartPage/CartItem.component.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ const CartItem = ({ item, products, handleRemoveProductClick, updateCart }) => {
2525
}*/
2626
// 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 )
2727
const newQty = event.target.value ? parseInt(event.target.value) : 1;
28+
2829
// Set the new quantity in state.
2930
setProductCount(newQty);
3031
if (products.length) {
3132
const updatedItems = getUpdatedItems(products, newQty, cartKey);
33+
3234
updateCart({
3335
variables: {
3436
input: {

components/Cart/CartPage/CartItemsContainer.component.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ const CartItemsContainer = () => {
7474
const updatedCart = getFormattedCart(data);
7575
localStorage.setItem('woocommerce-cart', JSON.stringify(updatedCart));
7676
// Update cart data in React Context.
77+
78+
79+
7780
setCart(updatedCart);
7881
},
7982
onError: (error) => {

utils/functions/functions.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,18 @@ export const getFormattedCart = (data) => {
198198
formattedCart.products = [];
199199
let totalProductsCount = 0;
200200

201-
for (let i = 0; i < givenProducts.length; i++) {
201+
let i = 0;
202+
givenProducts.forEach(() => {
202203
const givenProduct = givenProducts[i].product;
203204
const product = {};
204-
const total = getFloatVal(givenProducts[i].total);
205+
// Convert price to a float value
206+
const convertedCurrency = givenProducts[i].total.replace(/[^0-9.-]+/g, '');
205207

206208
product.productId = givenProduct.productId;
207209
product.cartKey = givenProducts[i].key;
208210
product.name = givenProduct.name;
209211
product.qty = givenProducts[i].quantity;
210-
product.price = total / product.qty;
212+
product.price = convertedCurrency / product.qty;
211213
product.totalPrice = givenProducts[i].total;
212214
product.image = {
213215
sourceUrl: givenProduct.image.sourceUrl,
@@ -216,14 +218,13 @@ export const getFormattedCart = (data) => {
216218
};
217219

218220
totalProductsCount += givenProducts[i].quantity;
219-
220221
// Push each item into the products array.
221222
formattedCart.products.push(product);
222-
}
223+
i++;
224+
});
223225

224226
formattedCart.totalProductsCount = totalProductsCount;
225227
formattedCart.totalProductsPrice = data.cart.total;
226-
227228
return formattedCart;
228229
};
229230

0 commit comments

Comments
 (0)