Просмотр исходного кода

feat module 6, 7 : remove sold out item or item not in menu from 'YOU MAY ALSO LIKE' section in product page

master
nadia 1 день назад
Родитель
Сommit
07096c5b76
1 измененных файлов: 21 добавлений и 3 удалений
  1. 21
    3
      src/components/ProductSuggestion/ProductSuggestion.jsx

+ 21
- 3
src/components/ProductSuggestion/ProductSuggestion.jsx Просмотреть файл

4
 import Grid from '@mui/material/Grid2';
4
 import Grid from '@mui/material/Grid2';
5
 import { useNavigate } from "react-router-dom";
5
 import { useNavigate } from "react-router-dom";
6
 import defaultImage from "../../assets/images/default.png"
6
 import defaultImage from "../../assets/images/default.png"
7
+import { getMenuCollectionTitlesByProductType } from '../../config/menuCollections';
8
+
9
+const normalizeTitle = (title = "") => title.trim().toUpperCase();
10
+
11
+const allowedSuggestionCollections = getMenuCollectionTitlesByProductType();
12
+
13
+// Function to check if a product is allowed in the suggestion based on its collections
14
+const isProductAllowedInSuggestion = (product) => {
15
+  const allowedCollections = allowedSuggestionCollections[product?.productType] || [];
16
+  const normalizedAllowedCollections = allowedCollections.map(normalizeTitle);
17
+
18
+  return product?.collections?.some((collection) =>
19
+    normalizedAllowedCollections.includes(normalizeTitle(collection?.title))
20
+  );
21
+};
7
 
22
 
8
 const ProductSuggestion = () => {
23
 const ProductSuggestion = () => {
9
 
24
 
19
         return shuffled.slice(0, num);
34
         return shuffled.slice(0, num);
20
       };
35
       };
21
 
36
 
22
-      const randomProducts = getRandomProducts(products, 4); // Select 4 random elements
37
+      const visibleProducts = products.filter(isProductAllowedInSuggestion);
38
+      // const randomProducts = getRandomProducts(products, 4);
39
+      const randomProducts = getRandomProducts(visibleProducts, 4); // Select 4 random visible products
23
       setSuggestProducts(randomProducts);
40
       setSuggestProducts(randomProducts);
24
     }
41
     }
25
   }, [products]);
42
   }, [products]);
170
             maxVariantPrice,
187
             maxVariantPrice,
171
             compareAtPriceRangeMinVariantPrice,
188
             compareAtPriceRangeMinVariantPrice,
172
             compareAtPriceRangeMaxVariantPrice,
189
             compareAtPriceRangeMaxVariantPrice,
173
-            productType,
174
-            variants,
190
+            // productType and variants are not needed here because filtering already happens before rendering.
191
+            // productType,
192
+            // variants,
175
             selected,
193
             selected,
176
           } = product;
194
           } = product;
177
 
195
 

Загрузка…
Отмена
Сохранить