From f013b51cda51146f5df71ded0eb77b21985a2bca Mon Sep 17 00:00:00 2001 From: Hlavtox Date: Tue, 18 Jul 2023 12:33:11 +0200 Subject: [PATCH] Improve availability filter --- src/Filters/Block.php | 15 +++++++++++---- src/Filters/Converter.php | 4 +++- src/Product/SearchProvider.php | 8 ++++++++ tests/php/FacetedSearch/Filters/BlockTest.php | 14 +++++++------- tests/php/FacetedSearch/Filters/ConverterTest.php | 8 ++++---- 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/Filters/Block.php b/src/Filters/Block.php index cbd16e11a..2f31dc2db 100644 --- a/src/Filters/Block.php +++ b/src/Filters/Block.php @@ -435,9 +435,9 @@ private function getAvailabilitiesBlock($filter, $selectedFilters) $availabilityOptions = []; if ($this->psStockManagement) { $availabilityOptions = [ - 0 => [ + 2 => [ 'name' => $this->context->getTranslator()->trans( - 'Not available', + 'In stock', [], 'Modules.Facetedsearch.Shop' ), @@ -451,9 +451,9 @@ private function getAvailabilitiesBlock($filter, $selectedFilters) ), 'nbr' => 0, ], - 2 => [ + 0 => [ 'name' => $this->context->getTranslator()->trans( - 'In stock', + 'Not available', [], 'Modules.Facetedsearch.Shop' ), @@ -509,6 +509,13 @@ private function getAvailabilitiesBlock($filter, $selectedFilters) } } } + + // Hide Available option if the count is the same as In stock, it doesn't make no sense + // Product count is a reliable indicator here, because there can never be product IN STOCK that is not AVAILABLE + // So if the counts match, it MUST BE the same products + if ($availabilityOptions[1]['nbr'] == $availabilityOptions[2]['nbr']) { + unset($availabilityOptions[1]); + } } $quantityBlock = [ diff --git a/src/Filters/Converter.php b/src/Filters/Converter.php index 1ee79b5ba..3650b6a70 100644 --- a/src/Filters/Converter.php +++ b/src/Filters/Converter.php @@ -168,7 +168,9 @@ public function getFacetsFromFilterBlocks(array $filterBlocks) $this->hideZeroValuesAndShowLimit($filters, (int) $filterBlock['filter_show_limit']); - if ((int) $filterBlock['filter_show_limit'] !== 0 || $filterBlock['type'] !== self::TYPE_ATTRIBUTE_GROUP) { + if ((int) $filterBlock['filter_show_limit'] !== 0 || + ($filterBlock['type'] !== self::TYPE_ATTRIBUTE_GROUP && $filterBlock['type'] !== self::TYPE_AVAILABILITY) + ) { usort($filters, [$this, 'sortFiltersByLabel']); } diff --git a/src/Product/SearchProvider.php b/src/Product/SearchProvider.php index f362e36b5..780d24f10 100644 --- a/src/Product/SearchProvider.php +++ b/src/Product/SearchProvider.php @@ -507,6 +507,7 @@ private function addEncodedFacetsToFilters(array $facets) private function hideUselessFacets(array $facets, $totalProducts) { foreach ($facets as $facet) { + // If the facet is a slider type, we hide it ONLY if the MIN and MAX value match if ($facet->getWidgetType() === 'slider') { $facet->setDisplayed( $facet->getProperty('min') != $facet->getProperty('max') @@ -514,6 +515,13 @@ private function hideUselessFacets(array $facets, $totalProducts) continue; } + // We won't apply this to availability facet, let's keep the value displayed + // Don't worry, the facet will be hidden if there are no values with products + if ($facet->getType() == 'availability') { + continue; + } + + // Now the rest of facets - we apply this logic $totalFacetProducts = 0; $usefulFiltersCount = 0; foreach ($facet->getFilters() as $filter) { diff --git a/tests/php/FacetedSearch/Filters/BlockTest.php b/tests/php/FacetedSearch/Filters/BlockTest.php index c3dcaf589..e803b950c 100644 --- a/tests/php/FacetedSearch/Filters/BlockTest.php +++ b/tests/php/FacetedSearch/Filters/BlockTest.php @@ -380,18 +380,18 @@ public function testGetFiltersBlockWithQuantities() 'id_key' => 0, 'name' => 'Availability', 'values' => [ - [ - 'name' => 'Not available', - 'nbr' => 1000, + 2 => [ + 'name' => 'In stock', + 'nbr' => 50, ], - [ + 1 => [ 'name' => 'Available', 'nbr' => 100, 'checked' => true, ], - [ - 'name' => 'In stock', - 'nbr' => 50, + 0 => [ + 'name' => 'Not available', + 'nbr' => 1000, ], ], 'filter_show_limit' => 0, diff --git a/tests/php/FacetedSearch/Filters/ConverterTest.php b/tests/php/FacetedSearch/Filters/ConverterTest.php index 2b9b6093f..d2508cd4e 100644 --- a/tests/php/FacetedSearch/Filters/ConverterTest.php +++ b/tests/php/FacetedSearch/Filters/ConverterTest.php @@ -424,14 +424,14 @@ public function facetsProvider() 'type' => 'availability', 'id_key' => 0, 'name' => 'Availability', 'values' => [ - 0 => [ - 'name' => 'Not available', - 'nbr' => 0, - ], 1 => [ 'name' => 'In stock', 'nbr' => 11, ], + 0 => [ + 'name' => 'Not available', + 'nbr' => 0, + ], ], 'filter_show_limit' => '0', 'filter_type' => '0',