Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve availability filter #883

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]);
}
matthieu-rolland marked this conversation as resolved.
Show resolved Hide resolved
}

$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