Skip to content

Commit

Permalink
Merge pull request #199 from w3bdesign/development
Browse files Browse the repository at this point in the history
Fix price bug with variable products
  • Loading branch information
w3bdesign authored Jul 6, 2020
2 parents 709688b + e744b0a commit d017798
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions components/Cart/Cart.component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 : '';
Expand Down
2 changes: 2 additions & 0 deletions components/Cart/CartPage/CartItem.component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
3 changes: 3 additions & 0 deletions components/Cart/CartPage/CartItemsContainer.component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
13 changes: 7 additions & 6 deletions utils/functions/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
};

Expand Down

1 comment on commit d017798

@vercel
Copy link

@vercel vercel bot commented on d017798 Jul 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.