To add custom conditions to product collection, you need to override build() function. I hope you have a custom module with basic skeletons like registration.php and module.xml file,
Path: app/code/Magemonkeys/FilterSearch/etc/frontend/di.xml
1 2 3 4 5 6 7 8 |
<?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="Magemonkeys_FilterSearch::custom_filterSearchr_conditions" type="Magemonkeys\FilterSearch\Plugin\CatalogSearch\Model\Search\IndexBuilder" /> </type> </config> |
Path: app/code/Magemonkeys/FilterSearch/Plugin/CatalogSearch/Model/Search/IndexBuilder.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php namespace Magemonkeys\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 magemonkeys */ $storeId = $this->storeManager->getStore()->getStoreId(); $query = 'WHERE_CUSTOM_CONDITION'; $select->where($query); /* customization by magemonkeys end here */ return $select; } } |
Please follow the below methods to get the attribute options...
Update product attribute value programmatically in Magento 2 . [crayon-62877b8c44900083499762/]...
If you want restrict customer to checkout based on your...
Sometime we need to set html data without load or...
If you want get query string params in controller file,...