Magento 2 – Add Search by Category in Advanced Search

advanced-searchcategorymagento2search

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

I know links for magento-1

1)add-search-by-category-in-advanced-search-in-magento-1

2)add-search-by-category-in-advanced-search-in-magento-1

How to do it in magento 2?

Best Answer

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

vendor/magento/core/module-catalog-search/Block/Advanced/Form.php
vendor/magento/core/module-catalog-search/Model/Advanced.php
vendor/magento/module-catalog-search/view/frontend/templates/advanced/form.phtml

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')
            ->addAttributeToSelect($this->_objectManager->get('Magento\Catalog\Model\Config')->getProductAttributes())
            ->addMinimalPrice()
            ->addStoreFilter();
        /* 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:

   <li>
        <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 ?>

        </select>
    </li> 

(Above code will show 1st level categories.)

Now You can see category filter in Advanced Search.

Related Topic