diff --git a/admin/update-token-session.php b/admin/update-token-session.php index 9162fc5..90017b6 100644 --- a/admin/update-token-session.php +++ b/admin/update-token-session.php @@ -1,5 +1,5 @@ "") { $_SESSION["isLoggedIn"] = false; } $productSearchResult = $_SESSION["productSearchResult"]; -// var_dump($product); -// if (isset($_SESSION["productSearchResult"])) { -// $productSearchResult = $_SESSION["productSearchResult"]; -// var_dump($productSearchResult) +// Filter products based on selected categories +// $category = strtolower(trim($_GET['category'])); + +$filteredProducts = []; + +// Check if the form is submitted and categories are selected +if ($_SERVER["REQUEST_METHOD"] == "POST") { + $minPrice = ($_POST['minPrice'] !== '' && is_numeric($_POST['minPrice'])) ? floatval($_POST['minPrice']) : null; + $maxPrice = ($_POST['maxPrice'] !== '' && is_numeric($_POST['maxPrice'])) ? floatval($_POST['maxPrice']) : null; + echo "Min Price: " . ($minPrice !== null ? $minPrice : "null") . "
"; + echo "Max Price: " . ($maxPrice !== null ? $maxPrice : "null") . "
"; + + // Check if categories are selected + if (isset($_POST['category'])) { + $selectedCategories = $_POST['category']; + foreach ($selectedCategories as $selectedCategory) { + $category = strtolower(trim($selectedCategory)); + foreach ($productSearchResult['results'] as $result) { + $productCategory = strtolower(trim($result['product']['product_category'])); + $productPrice = isset($result['product']['sale_price']) ? $result['product']['sale_price'] : $result['product']['regular_price']; + + // Set minPrice to 0 if it's null + if ($minPrice === null) { + $minPrice = 0; + } + + // Set maxPrice to a very high value if it's null + if ($maxPrice === null) { + $maxPrice = PHP_FLOAT_MAX; + } + + // Check if the product matches the selected category and falls within the price range + if ( + $productCategory == $category && + ($productPrice >= $minPrice) && + ($productPrice <= $maxPrice) + ) { + $filteredProducts[] = $result; + } + } + } + } else { + // If no category is selected, filter only by price range + foreach ($productSearchResult['results'] as $result) { + $productPrice = isset($result['product']['sale_price']) ? $result['product']['sale_price'] : $result['product']['regular_price']; + + // Set minPrice to 0 if it's null + if ($minPrice === null) { + $minPrice = 0; + } + + // Set maxPrice to a very high value if it's null + if ($maxPrice === null) { + $maxPrice = PHP_FLOAT_MAX; + } + + if (($productPrice >= $minPrice) && + ($productPrice <= $maxPrice) + ) { + $filteredProducts[] = $result; + } + } + } +} // If the request method is GET and filter parameters are present in the URL +// If the request method is GET and filter parameters are present in the URL +if (!empty($_GET['minPrice']) || !empty($_GET['maxPrice']) || !empty($_GET['category'])) { + // Initialize the filtered products array + $filteredProducts = []; + + // Get minPrice and maxPrice from the URL + $minPrice = isset($_GET['minPrice']) ? floatval($_GET['minPrice']) : null; + $maxPrice = isset($_GET['maxPrice']) ? floatval($_GET['maxPrice']) : null; + + // Set default values for minPrice and maxPrice if they are null + if ($minPrice === null) { + $minPrice = 0; + } + + if ($maxPrice === null) { + $maxPrice = PHP_FLOAT_MAX; + } + + // Get selected categories + $selectedCategories = isset($_GET['category']) ? $_GET['category'] : []; + + // Check if categories are selected + if (!empty($selectedCategories)) { + // Loop through each selected category + foreach ($selectedCategories as $selectedCategory) { + $category = strtolower(trim($selectedCategory)); + + // Loop through each product in the search result + foreach ($productSearchResult['results'] as $result) { + $product = $result['product']; + $productCategory = strtolower(trim($product['product_category'])); + $productPrice = isset($product['sale_price']) ? $product['sale_price'] : $product['regular_price']; + + // Check if the product matches the selected category and falls within the price range + if ( + $productCategory == $category && + ($productPrice >= $minPrice) && + ($productPrice <= $maxPrice) + ) { + $filteredProducts[] = $result; + } + } + } + } else { + // If no category is selected, filter only by price range + foreach ($productSearchResult['results'] as $result) { + $product = $result['product']; + $productPrice = isset($product['sale_price']) ? $product['sale_price'] : $product['regular_price']; + + // Check if the product falls within the price range + if (($productPrice >= $minPrice) && + ($productPrice <= $maxPrice) + ) { + $filteredProducts[] = $result; + } + } + } +} else { + // If no filter parameters are present, display all products + $filteredProducts = $productSearchResult['results']; +} + + ?> @@ -164,34 +287,17 @@ $productSearchResult = $_SESSION["productSearchResult"];
No products found
'; - // } else { - // foreach ($productSearchResult["results"] as $productItem) { - // $product = $productItem["product"]; - // $product_image = !empty($product["product_image"]) ? $product["product_image"] : "https://upload.wikimedia.org/wikipedia/commons/thumb/6/65/No-Image-Placeholder.svg/330px-No-Image-Placeholder.svg.png"; ?>
@@ -208,66 +314,36 @@ $productSearchResult = $_SESSION["productSearchResult"];
- -
"> -
- +
">
- 0) : ?> - Inquire Inquire -
- Showing - of item(s) + Showing - of item(s)
- -
- - -
-
-

Price

-
-
-
-
-
- - - -
-
+