How to add search by category to advanced search

Last modified by Rasel on Fri, June 25, 2010 09:30
Source|Old Revisions  

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:

  • app/code/core/Mage/CatalogSearch/Block/Advanced/Form.php
  • app/code/core/Mage/CatalogSearch/Model/Advanced.php
  • app/design/yourdesign/yourdesign/template/catalogsearch/advanced/form.phtml

At the very end of app/code/core/Mage/CatalogSearch/Block/Advanced/Form.php (before the closing brace), add:

  1.     public function getStoreCategories()
  2.     {
  3.         $helper = Mage::helper('catalog/category');
  4.         return $helper->getStoreCategories();
  5.     }

In app/code/core/Mage/CatalogSearch/Model/Advanced.php, replace the getSearchCriterias() function (line 157) with the code below:

  1. public function getSearchCriterias()
  2.     {
  3.         $search = $this->_searchCriterias;
  4.         /* display category filtering criteria */
  5.         if(isset($_GET['category']) && is_numeric($_GET['category'])) {
  6.             $category = Mage::getModel('catalog/category')->load($_GET['category']);
  7.             $search[] = array('name'=>'Category','value'=>$category->getName());
  8.         }
  9.         return $search;
  10.     }

replace the next function, getProductCollection(), with:

  1.   public function getProductCollection(){
  2.         if (is_null($this->_productCollection)) {
  3.             $this->_productCollection = Mage::getResourceModel('catalogsearch/advanced_collection')
  4.                 ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
  5.                 ->addMinimalPrice()
  6.                 ->addStoreFilter();
  7.                 Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($this->_productCollection);
  8.                 Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($this->_productCollection);
  9.             /* include category filtering */
  10.             if(isset($_GET['category']) && is_numeric($_GET['category'])) $this->_productCollection->addCategoryFilter(Mage::getModel('catalog/category')->load($_GET['category']),true);
  11.         }
  12.  
  13.         return $this->_productCollection;
  14.     }

In app/design/yourdesign/yourdesign/template/catalogsearch/advanced/form.phtml, after this code:

  1.         <?php foreach ($this->getSearchableAttributes() as $_attribute): ?>
  2.         <?php $_code = $_attribute->getAttributeCode() ?>
  3.         <li>
  4.             <label for="<?php echo $_code ?>"><?php echo $this->getAttributeLabel($_attribute) ?></label>
  5.             <?php switch($this->getAttributeInputType($_attribute)):
  6.                 case 'number': ?>
  7.                 <div class="range field-row">
  8.                     <input name="<?php echo $_code ?>[from]" value="<?php echo $this->htmlEscape($this->getAttributeValue($_attribute, 'from')) ?>" id="<?php echo $_code ?>" title="<?php echo $this->htmlEscape($this->getAttributeLabel($_attribute)) ?>"  class="input-text validate-number" type="text" />
  9.                     <input name="<?php echo $_code ?>[to]" value="<?php echo $this->htmlEscape($this->getAttributeValue($_attribute, 'to')) ?>" id="<?php echo $_code ?>_to" title="<?php echo $this->htmlEscape($this->getAttributeLabel($_attribute)) ?>"  class="input-text validate-number" type="text"/>
  10.                 </div>
  11.                 <?php break;
  12.                 case 'select': ?>
  13.                     <?php echo $this->getAttributeSelectElement($_attribute) ?>
  14.                 <?php break;
  15.                 case 'yesno': ?>
  16.                     <?php echo $this->getAttributeYesNoElement($_attribute) ?>
  17.                 <?php break;
  18.                 case 'date': ?>
  19.                     <?php echo $this->getDateInput($_attribute, 'from') ?>
  20.                     -
  21.                     <?php echo $this->getDateInput($_attribute, 'to') ?>
  22.                 <?php break;
  23.                 default: ?>
  24.                 <input name="<?php echo $_code ?>" id="<?php echo $_code ?>" value="<?php echo $this->htmlEscape($this->getAttributeValue($_attribute)) ?>" title="<?php echo $this->htmlEscape($this->getAttributeLabel($_attribute)) ?>"  class="input-text <?php echo $this->getAttributeValidationClass($_attribute) ?>" type="text" />
  25.             <?php endswitch; ?>
  26.         </li>
  27.         <?php endforeach; ?>

add:

  1.  
  2.         <li>
  3.             <label for="category_search_field">Search by Category:</label>
  4.             <select name="category" id="category_search_field">
  5.                 <option value="">-- Any Category --</option>
  6.                 <?php foreach ($this->getStoreCategories() as $_category): ?>
  7.                 <?php if($_category->hasChildren()): ?>
  8.                 <option class="parent-cat" value="<?php echo $_category->getId(); ?>"><?php echo $_category->getName();?></option>
  9.                 <?php foreach ($_category->getChildren() as $subcategory):
  10.                 if($subcategory->getIsActive()) : ?>
  11.                     <option value="<?php echo $subcategory->getId(); ?>"<?php echo ($this->getRequest()->getQuery('category') == $subcategory->getId() ? ' selected="selected"': "") ?>><?php echo $subcategory->getName(); ?></option>
  12.                 <?php endif; endforeach; ?>
  13.                 <?php elseif($_category->getIsActive()): ?>
  14.                 <option value="<?php echo $_category->getId(); ?>"><?php echo $_category->getName();?></option>
  15.                 <?php endif; ?>
  16.                 <?php endforeach ?>
  17.  
  18.             </select>
  19.         </li>

Now, If you are going to search only by Category then you will face an error message like “You have to specify at least one search term”. To solve it you need to modify following file:

  • app/code/core/Mage/CatalogSearch/Model/Advanced.php

Open the file and search this function addFilters. In this function you can see following codes, replace this

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

with following codes:

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

Now you won’t face any problem in advanced search only by Category or any single attribute search.




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs