Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Add category search option to advanced search Magento

Category search option to advanced search Magento


On the advanced search page, “search by category” is not an option by default, but can be very helpful. To add this, you will need to edit the following files:

Step 1. Required files in which we have to edit the code

app/code/core/Mage/CatalogSearch/Block/Advanced/Form.php
app/code/core/Mage/CatalogSearch/Model/Advanced.php
app/design/frontend/default/YourTheme/template/catalogsearch/advanced/form.phtml

Its a good practice to override your core file in local directory.

You can download here

Step 2. Navigate to the app/code/core/Mage/CatalogSearch/Block/Advanced/Form.php and open the file in the editor of your choice and add the below code very end of the file before closing the brace.

public function getStoreCategories()
{
$helper = Mage::helper('catalog/category');
return $helper->getStoreCategories();
}

Step 3.  Now navigate to the app/code/core/Mage/CatalogSearch/Model/Advanced.php, open the file in the editor of your choice and search for the getSearchCriterias() function and replace the getSearchCriterias() with the code below.

public function getSearchCriterias()
{
$search = $this->_searchCriterias;
/* for displaying the category filter */
if(isset($_GET['category']) && is_numeric($_GET['category'])) {
$category = Mage::getModel('catalog/category')->load($_GET['category']);
$search[] = array('name'=>'Category','value'=>$category->getName());
}
return $search;
}

and search for the getProductCollection(); function in the same file and replace the getProductCollection(); function with the code below.

public function getProductCollection()
{
if (is_null($this->_productCollection)) {
$this->_productCollection = Mage::getResourceModel('catalogsearch/advanced_collection')
        ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
        ->addMinimalPrice()
        ->addStoreFilter();
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection
       ($this->_productCollection);
       Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection
       ($this->_productCollection);
       /* adding category filter */
      if(isset($_GET['category']) && is_numeric($_GET['category']))
      $this->_productCollection-  >addCategoryFilter(Mage::getModel('catalog/category')
      - >load($_GET['category']),true);
}

return $this->_productCollection;
}

Step 4. Adding the Category dropdown for the frontend.

Now we have to display all the categories into the Advance Search from there user can select the Categories. For this navigate to the file app/design/frontend/default/your_custom_theme/template/catalogsearch/advanced/form.phtml or you can find this file app/design/frontend/base/default/template/catalogsearch/advanced/form.phtml search for the below code.

<!---Category Code Starts Here------>

<li>
<label for="category_search_field">Search by Category:</label>
<select name="category" id="category_search_field">
<option value="">-- Select Category --</option>
<?php foreach ($this->getStoreCategories() as $_category): ?>
<?php if($_category->hasChildren()): ?>
<option value="<?php echo $_category->getId(); ?>"><?php echo $_category->getName();?></option>
<?php foreach ($_category->getChildren() as $subcategory):
if($subcategory->getIsActive()) : ?>
<option value="<?php echo $subcategory->getId(); ?>"<?php echo ($this->getRequest()->getQuery('category') == $subcategory->getId() ? ' selected="selected"': "") ?>><?php echo $subcategory->getName(); ?></option>
<?php endif; endforeach; ?>
<?php elseif($_category->getIsActive()): ?>
<option value="<?php echo $_category->getId(); ?>"><?php echo $_category->getName();?></option>
<?php endif; ?>
<?php endforeach ?>

</select>
</li>

<!---Category Code Ends------------->


Step 5. Now Navigate to the app/code/core/Mage/CatalogSearch/Model/Advanced.php and open the file in the editor of your choice and search for the addFilters() function and you will see the following codes, replace this one

if ($allConditions) {
$this->getProductCollection()->addFieldsToFilter($allConditions);
} else if (!count($filteredAttributes)) {
Mage::throwException(Mage::helper('catalogsearch')->__('You have to specify at least one search term'));
}

with the below codes.

if (($allConditions) || (isset($values['category']) && is_numeric($values['category']))) {
$this->getProductCollection()->addFieldsToFilter($allConditions);
} else if (!count($filteredAttributes)) {
Mage::throwException(Mage::helper('catalogsearch')->__('You have to specify at least one search term'));
}

 

Copyright @ 2017 HKBlog.