From 90dbc0210152605b22d8b59bf87d3ee80a8d8779 Mon Sep 17 00:00:00 2001 From: Jun Barroga Date: Tue, 20 Feb 2024 18:22:42 +0800 Subject: [PATCH 1/2] Search filter --- admin/update-token-session.php | 2 +- functions.php | 2 +- shop-list-left-sidebar.php | 327 ++++++++++++++++++++------------- update-token-session.php | 2 +- 4 files changed, 205 insertions(+), 128 deletions(-) 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

-
-
-
-
-
- - - -
-
+ -
@@ -342,22 +318,15 @@ if (!empty($_GET['minPrice']) || !empty($_GET['maxPrice']) || !empty($_GET['cate
    1) { ?>
  • 1
  • 2) { ?>
  • ...
  • @@ -365,25 +334,21 @@ if (!empty($_GET['minPrice']) || !empty($_GET['maxPrice']) || !empty($_GET['cate } } - // Display pagination links within the defined range for ($page = $startPage; $page <= $endPage; $page++) { $activeClass = ($page == $currentPage) ? 'active' : ''; $filterParams = array('minPrice' => $minPrice, 'maxPrice' => $maxPrice); if (isset($_POST['category'])) { $filterParams['category'] = $_POST['category']; } - // Modify the pagination URL to include the current page number and category filter - $paginationUrl = '?' . http_build_query(array_merge($_GET, array('page' => $page, 'category' => $selectedCategories, 'minPrice' => $minPrice, 'maxPrice' => $maxPrice))); + $paginationUrl = '?' . http_build_query(array_merge($_GET, $_POST, array('page' => $page))); + - // Output the pagination link ?>
  • ...
  • @@ -414,7 +379,68 @@ if (!empty($_GET['minPrice']) || !empty($_GET['maxPrice']) || !empty($_GET['cate

    Category

-
$_POST['category'], 'minPrice' => $_POST['minPrice'], 'maxPrice' => $_POST['maxPrice']))); ?>"> + + + + +
+ -
  • @@ -472,12 +496,11 @@ if (!empty($_GET['minPrice']) || !empty($_GET['maxPrice']) || !empty($_GET['cate
  • - - + -->
    - +