Skip to content

Commit

Permalink
Merge pull request #883 from Hlavtox/merge-availability
Browse files Browse the repository at this point in the history
Improve availability filter
  • Loading branch information
Hlavtox authored Jul 24, 2023
2 parents 2c16e24 + f013b51 commit d429b5e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
15 changes: 11 additions & 4 deletions src/Filters/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
),
Expand All @@ -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'
),
Expand Down Expand Up @@ -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 = [
Expand Down
4 changes: 3 additions & 1 deletion src/Filters/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

Expand Down
8 changes: 8 additions & 0 deletions src/Product/SearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,13 +507,21 @@ 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')
);
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) {
Expand Down
14 changes: 7 additions & 7 deletions tests/php/FacetedSearch/Filters/BlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions tests/php/FacetedSearch/Filters/ConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit d429b5e

Please sign in to comment.