Magento – Magento-2 How to get custom product collection in catalog search result based on particular category

catalogsearchcollection;magento2

I am working on custom module for catalog access. It requires only logged in customer to access the assigned category products.

Right now in catalog search result, product collection displays all the category products, but I need to display only selected category products.

See below Example

Suppose customer A has access to only categories Men > Tops > Tshirts. So, he will only see Men > Tops > Tshirts products in catalog search in magento 2. In search results only assigned categories products should display and not any other category products.

Best Answer

I had a similar requirement to restrict the access to products in search result based on some customer attribute.

There are 2 classes responsible to filter and build the query for catalog search namely Magento\CatalogSearch\Model\Search\IndexBuilder and Magento\Framework\Search\Adapter\Mysql\Query\Builder\Match, You can add plugin/override to the methods IndexBuilder::build and the method Magento\Framework\Search\Adapter\Mysql\Query\Builder\Match::build to apply the required logic.

Related Topic