How to show categories on the top search bar results?

Do not want to interact with ElasticSearch as using MySQL search engine.

Any sort of note would be much helpful.

1 Answer 1


Reference Link

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.

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.