Skip to content

Commit 5e16bef

Browse files
committed
Fix all known bugs with variable products
1 parent 0472c50 commit 5e16bef

File tree

5 files changed

+29
-12
lines changed

5 files changed

+29
-12
lines changed

components/Cart/CartPage/CartItem.component.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const CartItem = ({
2929
return;
3030
}
3131
// 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 )
32-
const newQty = event.target.value ? parseInt(event.target.value) : 1;
32+
const newQty = event.target.value ? parseInt(event.target.value, 10) : 1;
3333

3434
// Set the new quantity in state.
3535
setProductCount(newQty);

components/Product/IndexProducts.component.jsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { filteredVariantPrice } from 'utils/functions/functions';
1111
* @param {Object} products
1212
*/
1313
const IndexProducts = ({ products }) => {
14+
console.log(products);
1415
return (
1516
<>
1617
<section className="container mx-auto bg-white">
@@ -26,6 +27,7 @@ const IndexProducts = ({ products }) => {
2627
onSale,
2728
slug,
2829
image,
30+
variations,
2931
}) => (
3032
<div
3133
key={uuidv4()}
@@ -72,11 +74,13 @@ const IndexProducts = ({ products }) => {
7274
{onSale && (
7375
<>
7476
<div className="flex justify-center">
75-
<div className="pt-1 text-gray-900 line-through">
76-
{regularPrice}
77+
<div className="pt-1 text-gray-900">
78+
{variations && filteredVariantPrice(price)}
79+
{!variations && salePrice}
7780
</div>
78-
<div className="pt-1 ml-2 text-gray-900">
79-
{filteredVariantPrice(salePrice)}
81+
<div className="pt-1 ml-2 text-gray-900 line-through">
82+
{variations && filteredVariantPrice(price, 'right')}
83+
{!variations && regularPrice}
8084
</div>
8185
</div>
8286
</>

components/Product/SingleProduct.component.jsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,14 @@ const SingleProduct = ({ product }) => {
7272
{onSale && (
7373
<>
7474
<div className="flex">
75-
<p className="pt-1 mt-4 text-3xl text-gray-900">
76-
{filteredVariantPrice(salePrice)}
75+
<p className="pt-1 mt-4 text-3xl text-gray-900">
76+
{product.variations && filteredVariantPrice(price)}
77+
{!product.variations && salePrice}
7778
</p>
7879
<p className="pt-1 pl-8 mt-4 text-2xl text-gray-900 line-through">
79-
{regularPrice}
80+
{product.variations &&
81+
filteredVariantPrice(price, 'right')}
82+
{!product.variations && regularPrice}
8083
</p>
8184
</div>
8285
</>

utils/functions/functions.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ export const trimmedStringToLength = (string, length) => {
1717
};
1818

1919
/**
20-
* Filter variant price. Changes from "kr198.00 - kr299.00" to kr299.00
20+
* Filter variant price. Changes "kr198.00 - kr299.00" to kr299.00 or kr198 depending on the side variable
21+
* @param {String} side Which side of the string to return (which side of the "-" symbol)
2122
* @param {String} price The inputted price that we need to convert
2223
*/
23-
export const filteredVariantPrice = (price) => {
24-
// Filter price from "kr198.00 - kr299.00" to kr299.00
25-
return price.substring(price.length, price.indexOf('-')).replace('-', '');
24+
export const filteredVariantPrice = (price, side) => {
25+
if ('right' === side) {
26+
return price.substring(price.length, price.indexOf('-')).replace('-', '');
27+
}
28+
return price.substring(0, price.indexOf('-')).replace('-', '');
2629
};
2730

2831
/**

utils/gql/GQL_QUERIES.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ export const FETCH_ALL_PRODUCTS_QUERY = gql`
126126
price
127127
regularPrice
128128
salePrice
129+
variations {
130+
nodes {
131+
price
132+
regularPrice
133+
salePrice
134+
}
135+
}
129136
}
130137
}
131138
}

0 commit comments

Comments
 (0)