Skip to content

Commit a557ba2

Browse files
authored
Merge pull request #375 from w3bdesign/develop
Add filter for variant price
2 parents a882ed9 + 0472c50 commit a557ba2

File tree

7 files changed

+42
-155
lines changed

7 files changed

+42
-155
lines changed

components/AlgoliaSearch/SearchResults.component.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import Link from 'next/link';
22

3+
import { trimmedStringToLength } from 'utils/functions/functions';
4+
35
/**
46
* Displays search results from Algolia
57
* @param {Object} hit {
@@ -55,7 +57,9 @@ const SearchResults = ({
5557
)}
5658
{!on_sale && <span className="text-base">kr {regular_price}</span>}
5759
<br />
58-
<span className="text-base">{short_description}</span>
60+
<span className="text-base">
61+
{trimmedStringToLength(short_description, 30)}
62+
</span>
5963
</div>
6064
</div>
6165
</Link>

components/Cart/CartPage/MobileCartItem.component.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const MobileCartItem = ({
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
// Set the new quantity in state.
3434
setProductCount(newQty);
3535
if (products.length) {

components/Product/IndexProducts.component.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { v4 as uuidv4 } from 'uuid';
33

44
import WOO_CONFIG from 'utils/config/nextConfig';
55

6+
import { filteredVariantPrice } from 'utils/functions/functions';
7+
68
/**
79
* Displays all of the products as long as length is defined.
810
* Does a map() over the props array and utilizes uuidv4 for unique key values.
@@ -74,7 +76,7 @@ const IndexProducts = ({ products }) => {
7476
{regularPrice}
7577
</div>
7678
<div className="pt-1 ml-2 text-gray-900">
77-
{salePrice}
79+
{filteredVariantPrice(salePrice)}
7880
</div>
7981
</div>
8082
</>

components/Product/SingleProduct.component.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import LoadingSpinner from 'components/LoadingSpinner/LoadingSpinner.component';
55

66
import WOO_CONFIG from 'utils/config/nextConfig';
77

8+
import { filteredVariantPrice } from 'utils/functions/functions';
9+
810
/**
911
* Shows a single product with an Add To Cart button.
1012
* Uses GraphQL for product data
@@ -70,8 +72,8 @@ const SingleProduct = ({ product }) => {
7072
{onSale && (
7173
<>
7274
<div className="flex">
73-
<p className="pt-1 mt-4 text-3xl text-gray-900">
74-
{salePrice}
75+
<p className="pt-1 mt-4 text-3xl text-gray-900">
76+
{filteredVariantPrice(salePrice)}
7577
</p>
7678
<p className="pt-1 pl-8 mt-4 text-2xl text-gray-900 line-through">
7779
{regularPrice}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nextjs-woocommerce",
3-
"version": "1.0.13",
3+
"version": "1.0.14",
44
"description": "NextJS WooCommerce webshop",
55
"main": "index.js",
66
"scripts": {

utils/apollo/ApolloClient.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,16 @@ export const afterware = new ApolloLink((operation, forward) => {
7070
});
7171
});
7272

73+
const clientSide = typeof window === 'undefined';
74+
7375
// Apollo GraphQL client.
7476
const client = new ApolloClient({
75-
ssrMode: typeof window === 'undefined',
77+
ssrMode: clientSide,
7678
link: middleware.concat(
7779
afterware.concat(
7880
createHttpLink({
7981
uri: WOO_CONFIG.GRAPHQL_URL,
80-
fetch: fetch,
82+
fetch,
8183
})
8284
)
8385
),

utils/functions/functions.js

Lines changed: 24 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,38 @@ import { v4 as uuidv4 } from 'uuid';
33
import WOO_CONFIG from 'utils/config/nextConfig';
44

55
/**
6-
* Convert price from string to floating value and convert it to use two decimals
7-
* @param {String} string
6+
* Shorten inputted string (usually product description) to a maximum of 20 characters
7+
* @param {String} string The string that we input
8+
* @param {Integer} length The length that we want to shorten the text to
89
*/
9-
export const getFloatVal = (string) => {
10-
const stringWithoutKr = string.substring(2);
11-
const floatValue = parseFloat(stringWithoutKr);
12-
return null !== floatValue
13-
? parseFloat(parseFloat(floatValue).toFixed(2))
14-
: '';
10+
export const trimmedStringToLength = (string, length) => {
11+
if (string.length > length) {
12+
const subStr = string.substring(0, length);
13+
return subStr + '...';
14+
} else {
15+
return string;
16+
}
1517
};
1618

1719
/**
18-
* Add first product to shopping cart
19-
* @param {Object} product
20+
* Filter variant price. Changes from "kr198.00 - kr299.00" to kr299.00
21+
* @param {String} price The inputted price that we need to convert
2022
*/
21-
export const addFirstProduct = (product) => {
22-
const { price } = product.product.products.edges[0].node;
23-
const productData = product.product.products.edges[0].node;
24-
let productPrice = getFloatVal(price);
25-
26-
// If no item in cart, push first product to an empty array
27-
let newCart = {
28-
products: [],
29-
totalProductsCount: 1,
30-
totalProductsPrice: productPrice,
31-
};
32-
33-
const newProduct = createNewProduct(productData, productPrice, 1);
34-
newCart.products.push(newProduct);
35-
36-
localStorage.setItem('woocommerce-cart', JSON.stringify(newCart));
37-
return newCart;
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('-', '');
3826
};
3927

4028
/**
41-
* Create a new product object
42-
* @param {Object} product
43-
* @param {Number} productPrice
44-
* @param {Number} quantity
45-
* @return { productId, image, name, price, quantity, totalPrice}
29+
* Convert price from string to floating value and convert it to use two decimals
30+
* @param {String} string
4631
*/
47-
export const createNewProduct = (product, productPrice, quantity) => {
48-
return {
49-
productId: product.productId,
50-
image: product.image.sourceUrl,
51-
name: product.name,
52-
price: productPrice,
53-
quantity: quantity,
54-
totalPrice: parseFloat((quantity * productPrice).toFixed(2)),
55-
};
32+
export const getFloatVal = (string) => {
33+
const stringWithoutKr = string.substring(2);
34+
const floatValue = parseFloat(stringWithoutKr);
35+
return null !== floatValue
36+
? parseFloat(parseFloat(floatValue).toFixed(2))
37+
: '';
5638
};
5739

5840
/**
@@ -75,111 +57,6 @@ export const updateCart = (
7557
);
7658
};
7759

78-
/**
79-
* Get updated products array
80-
* Update the product if it exists else,
81-
* add the new product to existing cart,
82-
*
83-
* @param {Object} existingProductsInCart Existing product in cart
84-
* @param {Object} product Product
85-
* @param {Integer} qtyToBeAdded Quantity
86-
* @param {Integer} newQty New qty of the product (optional)
87-
* @return {*[]}
88-
*/
89-
export const getUpdatedProducts = (
90-
existingProductsInCart,
91-
product,
92-
qtyToBeAdded,
93-
newQty = false
94-
) => {
95-
// Check if the product already exits in the cart.
96-
const productExistsIndex = isProductInCart(
97-
existingProductsInCart,
98-
product.productId
99-
);
100-
101-
// If product exists ( index of that product was found in the array ), update the product quantity and totalPrice
102-
if (-1 < productExistsIndex) {
103-
let updatedProducts = existingProductsInCart;
104-
let updatedProduct = updatedProducts[productExistsIndex];
105-
106-
// If have new quantity of the product available, set that, else add the qtyToBeAdded
107-
updatedProduct.qty = newQty
108-
? parseInt(newQty)
109-
: parseInt(updatedProduct.qty + qtyToBeAdded);
110-
updatedProduct.totalPrice = parseFloat(
111-
(updatedProduct.price * updatedProduct.qty).toFixed(2)
112-
);
113-
return updatedProducts;
114-
}
115-
// If product not found push the new product to the existing product array.
116-
let productPrice = getFloatVal(product.price);
117-
const newProduct = createNewProduct(product, productPrice, qtyToBeAdded);
118-
existingProductsInCart.push(newProduct);
119-
120-
return existingProductsInCart;
121-
};
122-
123-
/**
124-
* Returns index of the product if it exists.
125-
*
126-
* @param {Object} existingProductsInCart Existing Products.
127-
* @param {Integer} productId Product id.
128-
* @return {number | *} Index Returns -1 if product does not exist in the array, index number otherwise
129-
*/
130-
const isProductInCart = (existingProductsInCart, productId) => {
131-
const returnItemThatExits = (item) => {
132-
if (productId === item.productId) {
133-
return item;
134-
}
135-
return -1;
136-
};
137-
138-
// This new array will only contain the product which is matched.
139-
const newArray = existingProductsInCart.filter(returnItemThatExits);
140-
141-
return existingProductsInCart.indexOf(newArray[0]);
142-
};
143-
144-
/**
145-
* Remove item from the cart.
146-
*
147-
* @param {Integer} productId Product Id.
148-
* @return {any | string} Updated cart
149-
*/
150-
export const removeItemFromCart = (productId) => {
151-
let existingCart = localStorage.getItem('woo-next-cart');
152-
existingCart = JSON.parse(existingCart);
153-
154-
// If there is only one item in the cart, delete the cart.
155-
if (1 === existingCart.products.length) {
156-
localStorage.removeItem('woo-next-cart');
157-
return null;
158-
}
159-
160-
// Check if the product already exits in the cart.
161-
const productExistsIndex = isProductInCart(existingCart.products, productId);
162-
163-
// If product to be removed exits
164-
if (-1 < productExistsIndex) {
165-
const productTobeRemoved = existingCart.products[productExistsIndex];
166-
const qtyToBeRemovedFromTotal = productTobeRemoved.qty;
167-
const priceToBeDeductedFromTotal = productTobeRemoved.totalPrice;
168-
169-
// Remove that product from the array and update the total price and total quantity of the cart
170-
let updatedCart = existingCart;
171-
updatedCart.products.splice(productExistsIndex, 1);
172-
updatedCart.totalProductsCount =
173-
updatedCart.totalProductsCount - qtyToBeRemovedFromTotal;
174-
updatedCart.totalProductsPrice =
175-
updatedCart.totalProductsPrice - priceToBeDeductedFromTotal;
176-
177-
localStorage.setItem('woo-next-cart', JSON.stringify(updatedCart));
178-
return updatedCart;
179-
}
180-
return existingCart;
181-
};
182-
18360
/**
18461
* Returns cart data in the required format.
18562
* @param {String} data Cart data
@@ -292,7 +169,7 @@ export const getUpdatedItems = (products, newQty, cartKey) => {
292169
if (cartItem.cartKey === cartKey) {
293170
updatedItems.push({
294171
key: cartItem.cartKey,
295-
quantity: parseInt(newQty),
172+
quantity: parseInt(newQty, 10),
296173
});
297174

298175
// Otherwise just push the existing qty without updating.

0 commit comments

Comments
 (0)