On advanced search page, “search by category” is not an option by default.

I know links for magento-1



How to do it in magento 2?

1 Answer 1


To add this, you will need to override the following files:


At below code in Form.php, add:

protected $_categoryHelper;

public function __construct(
    \Magento\Catalog\Helper\Category $categoryHelper
) {
    $this->_categoryHelper = $categoryHelper;

Add below function in Form.php:

public function getStoreCategories()
   return $this->_categoryHelper->getStoreCategories();

In Advanced.php, replace the getSearchCriterias() function with the code below:

public function getSearchCriterias()
    $search = $this->_searchCriterias;
    /* display category filtering criteria */
    if(isset($_GET['cat']) && is_numeric($_GET['cat'])) {
        $category = $this->_objectManager->get('Magento\Catalog\Model\Category')->load($_GET['cat']);
        $search[] = array('name'=>'cat','value'=>$category->getName());
    return $search;

replace getProductCollection() function, with:

public function getProductCollection()
    if ($this->_productCollection === null) {
        $this->_productCollection = $this->_objectManager->get('Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection')
        /* need to include product active and visibility filtering here*/    
        /* include category filtering */
        if(isset($_GET['cat']) && is_numeric($_GET['cat'])) $this->_productCollection->addCategoryFilter($this->_objectManager->get('Magento\Catalog\Model\Category')->load($_GET['cat']),true);

    return $this->_productCollection;

Now search this function addFilters. In this function you can see following condition code,

if ($allConditions)

replace above with following code:

if ($allConditions || (isset($values['cat']) && is_numeric($values['cat'])) )

In form.phtml, after endforeach line i.e. line no 122 add below code:

        <label for="category_search_field">Search by Category:</label>
        <select name="cat" id="category_search_field">
            <option value="">-- Any Category --</option>
            <?php foreach ($this->getStoreCategories() as $_category): ?>

            <?php if($_category->hasChildren()): ?>
                  <option class="parent-cat" value="<?php echo $_category->getId(); ?>"><?php echo $_category->getName();?></option>
              <?php if($_category->getIsActive()) : ?>
                        <option value="<?php echo $_category->getId(); ?>"<?php echo ($this->getRequest()->getQuery('cat') == $_category->getId() ? ' selected="selected"': "") ?>><?php echo $_category->getName(); ?></option>
            <?php endif; ?>
            <?php elseif($_category->getIsActive()): ?>
            <option value="<?php echo $_category->getId(); ?>"><?php echo $_category->getName();?></option>
            <?php endif; ?>
            <?php endforeach ?>


(Above code will show 1st level categories.)

Now You can see category filter in Advanced Search.

  • 1
    There's literally no reason to edit the core in Magento 2.
    – benmarks
    Jun 19, 2017 at 6:50
  • 2
    @benmarks This changes made for testing purpose, after testing and seems like working fine then all files were overrided. And plz see my answer's first line.
    – Nitesh
    Jun 19, 2017 at 6:54

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.