In Magento 2, There are some situations where you need to set custom conditions for layered(filter) navigation. In Magento 2, All product collection comes from Catalog Module at Layer.php file from vendor/magento/module-catalog/Model/Layer.php.
Layer.php file has a function called getProductCollection() and its responsibility for getting all product collection in listing page.
While you gave custom conditions for getProductCollection() collection, You got all the product based on custom conditions you applied but that custom conditions don’t apply to Filter(Layered) Navigation to the listing.
If you are dealing with custom conditions in listing page and your conditions don’t apply to layered navigation filter.
When customer faced a problem for filtering in the listing, its feel bad experience for the site and for overcoming this issue for layered navigation we need to sort out the issue for run smooth functionality on the page.
After debugging we have found the actual file where custom conditions are worked for layered navigation.
If you want to give custom conditions for layered navigation you need to go for core file in Catalog Search Module, vendor/magento/module-catalog-search/Model/Search/IndexBuilder.php
build() function is the responsible for getting layered navigation content in listing and search result page from IndexBuilder.php
For Apply custom conditions to layered navigation, you need to override build() function. I hope you have a custom module with basic skeletons like registration.php and module.xml file,
File path,
app/code/Rbj/FilterSearch/etc/frontend/di.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <!-- Magento Catalog search module's IndexBuilder.php plugin file --> <type name="Magento\CatalogSearch\Model\Search\IndexBuilder"> <plugin name="Rbj_FilterSearch::custom_filterSearchr_conditions" type="Rbj\FilterSearch\Plugin\CatalogSearch\Model\Search\IndexBuilder" /> </type> </config>
File Path,
app/code/Rbj/FilterSearch/Plugin/CatalogSearch/Model/Search/IndexBuilder.php
<?php namespace Rbj\FilterSearch\Plugin\CatalogSearch\Model\Search; use Magento\Framework\Search\RequestInterface; class IndexBuilder { public function __construct( \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory ) { $this->storeManager = $storeManager; $this->_productCollectionFactory = $productCollectionFactory; } public function aroundBuild(\Magento\CatalogSearch\Model\Search\IndexBuilder $subject, callable $proceed, RequestInterface $request) { $select = $proceed($request); /* customization by rakesh jesadiya */ $storeId = $this->storeManager->getStore()->getStoreId(); $query = 'WHERE_CUSTOM_CONDITION'; $select->where($query); /* customization by rakesh jesadiya end here */ return $select; } }
You can give your custom filter conditions in aroundBuild() function in the above file.
Where $select = $proceed($request); Its display actual default query of Magento and you can give some extra conditions to based on your requirement.
Once You gave conditions to the above plugin you don’t need to give conditions or override getProductCollection() function.