cancel
Showing results for 
Search instead for 
Did you mean: 

How to add/integrate category search in advanced search?

How to add/integrate category search in advanced search?

How to add/integrate category search in advanced search in magento 2?

2 REPLIES 2

Re: How to add/integrate category search in advanced search?

Did you figure this out?  Does Magento2 allow searching of category data in the keyword search?  My install isn't returning category search results.  

Re: How to add/integrate category search in advanced search?

Hi @saikat _saha 

 

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

Now You can see category filter in Advanced Search.

 

Issue resolved?
Please click on Kudos & Accept as Solution!

 

 

Problem solved? Click Accept as Solution!