Skip to content
Open
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
20 changes: 12 additions & 8 deletions src/Product/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ public function initSearch($selectedFilters)
// Adds filters that specific for this controller
$this->addControllerSpecificFilters();

// Let modules add their own filters to the adapter. This is dispatched here, after the
// controller specific filters, rather than inside addControllerSpecificFilters() because
// that method has early returns (e.g. an already selected "Categories" facet) that would
// otherwise silently skip the hook and drop the module filters.
Hook::exec(
'actionFacetedSearchFilters',
[
'search' => $this,
'query' => $this->query,
]
);

// Add group by to remove duplicate values
$this->getSearchAdapter()->addGroupBy('id_product');

Expand Down Expand Up @@ -451,14 +463,6 @@ private function addControllerSpecificFilters()
empty($productPool) ? ['NULL'] : $productPool
);
}

Hook::exec(
'actionFacetedSearchFilters',
[
'search' => $this,
'query' => $this->query,
]
);
}

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/php/FacetedSearch/Product/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1192,4 +1192,28 @@ public function testAddFilter()
$this->search->addFilter('weight', [10, 20]);
$this->search->addFilter('id_feature', [[10, 20]]);
}

/**
* The actionFacetedSearchFilters hook must be dispatched even when a controller specific
* filter causes an early return inside addControllerSpecificFilters(): on a category query,
* selecting a "Categories" facet sets an id_category filter and used to skip the hook.
*
* @see https://github.com/PrestaShop/ps_facetedsearch/issues/1239
*/
public function testInitSearchDispatchesFacetedSearchFiltersHookWithSelectedCategoryFacet()
{
$hookCalls = [];
$hookMock = Mockery::mock(Hook::class);
$hookMock->shouldReceive('exec')
->andReturnUsing(function ($name) use (&$hookCalls) {
$hookCalls[] = $name;

return [];
});
Hook::setStaticExpectations($hookMock);

$this->search->initSearch(['category' => [[6]]]);

$this->assertContains('actionFacetedSearchFilters', $hookCalls);
}
}
Loading