I want to add some conditions for the layer navigation counter. Basically I want to skip from counting some products.

In vendor/magento/module-catalog-search/Model/Layer/Filter/Category.php in _getItemsData we have a products collection, and then based on the collection, all the counter for the categories comes form this line :

 $optionsFacetedData = $productCollection->getFacetedData('category');

On the getFacetedData function we have:

 $aggregations = $this->searchResult->getAggregations();

This will return all the counters for all categories, attributes, prices etc...

If I add some condition on the product collection nothing happen with the counter, even if i remove all the items from the collection i still have the same numbers for category counter.

Does anyone know how this getAggregations function works, and how the counter works?

I mean where in the code i can find the actual assignment for the category x and the number of products associated with the category.

1 Answer 1


The getAggregations function gets it's data from Magento's search framework, which works in a counter-intuitive way and fills the aggregation buckets for every search in advance. I suspect this is because it creates a temporary table each time and has to perform all operations before the transaction ends and disposes of the table. It doesn't use your product collection for this which is why the numbers don't change.

I had to fix a similar problem with a product collection and inspired by an answer to Perform catalog search in my custom table I did this:


<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\CatalogSearch\Model\Search\IndexBuilder">
        <plugin name="Namespace_Modulename::custom_collection" type="Namespace\Modulename\Plugin\CatalogSearch\Model\Search\IndexBuilder" />


namespace Namespace\Modulename\Plugin\CatalogSearch\Model\Search;

class IndexBuilder {   
    public function afterBuild(
        \Magento\CatalogSearch\Model\Search\IndexBuilder $subject,
        \Magento\Framework\Db\Select $select
    ) {
            // use your real table name here
            ['alias' => 'table_name'],
            // search_index is the existing table and the following condition filters it
            'search_index.entity_id = alias.product_id AND alias.enabled = true',
            // additional columns not applicable
        return $select;

Additionally, it appears the product collection is derived from the same search index, which is now being altered by the above code. In my case it made my other alteration to the product collection redundant. This much was all I needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.