How to add search by multiple categories to advanced search

Last modified by prasanthi on Fri, June 29, 2012 05:41
Source|Old Revisions  

Please see http://www.magentocommerce.com/wiki/how-to/how_to_add_search_by_category_to_advanced_search before reading this article. this is just a modification of that code.

This code checks if we have been asked to check for 2 categories, if we have it slips them up and adds them both as criteria.

First we need to add a function to return either an array or a string for testing and the end of app/code/core/Mage/CatalogSearch/Model/Advanced.php add this function,

  1.    public function getCatsFromSearchUrl($value){
  2.       $c = explode(',',$value);
  3.       if(count($c) > 1){ return $c; } else { return $value; }
  4.     }

and then from jakilcz code, edit his two functions two,

  1.     public function getSearchCriterias()
  2.         {
  3.             $search = $this->_searchCriterias;
  4.             /* display category filtering criteria */
  5.             if(isset($_GET['category'])) {
  6.                 $value = $this->getCatsFromSearchUrl($_GET['category']);
  7.                 if(is_array($value)){
  8.                   foreach($value as $v){
  9.                     if(is_numeric($v)){
  10.                       $category = Mage::getModel('catalog/category')->load($v);
  11.                       $search[] = array('name'=>'Category','value'=>$category->getName());
  12.                     }
  13.                   }
  14.                 } else {
  15.                   if(is_numeric($value)){
  16.                    $category = Mage::getModel('catalog/category')->load($value);
  17.                    $search[] = array('name'=>'Category','value'=>$category->getName());
  18.                   }
  19.                 }
  20.             }
  21.             return $search;
  22.         }

and

  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'])){
  11.               $value = $this->getCatsFromSearchUrl($_GET['category']);
  12.               if(is_array($value)){
  13.                 foreach($value as $v){ if(is_numeric($v)){ $this->_productCollection->addCategoryFilter(Mage::getModel('catalog/category')->load($v),true); } }
  14.               } else { if(is_numeric($value)){ $this->_productCollection->addCategoryFilter(Mage::getModel('catalog/category')->load($value),true); } }
  15.             }
  16.         }
  17.  
  18.         return $this->_productCollection;
  19.     }

then you can supply advanced search with &category=1,2,4

Note: this script checks if the products are in all categories and does not return products from each. as in a product must be in all listed categories to appear in results.

You can return the products from each category just modifying the getProductCollection from wordsbyalan & jakilcz in app/code/core/Mage/CatalogSearch/Model/Advanced.php like this:

  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'])){
  11.               $value = $this->getCatsFromSearchUrl($_GET['category']);
  12.               if(is_array($value)){
  13.                  $this->_productCollection->joinField('category_id', 'catalog/category_product_index', 'category_id', 'product_id = entity_id', null, 'left')->addAttributeToFilter('category_id', array('in' => $value));
  14.                 }
  15.               } else { if(is_numeric($value)){ $this->_productCollection->addCategoryFilter(Mage::getModel('catalog/category')->load($value),true); } }
  16.             }
  17.         }
  18.  
  19.         return $this->_productCollection;
  20.     }

Note: the $value is an array with the category ids comma separated (1,2,4)




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs