How to add search by multiple categories to advanced search
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,
- public function getCatsFromSearchUrl($value){
- $c = explode(',',$value);
- if(count($c) > 1){ return $c; } else { return $value; }
- }
and then from jakilcz code, edit his two functions two,
- public function getSearchCriterias()
- {
- $search = $this->_searchCriterias;
- /* display category filtering criteria */
- if(isset($_GET['category'])) {
- $value = $this->getCatsFromSearchUrl($_GET['category']);
- if(is_array($value)){
- foreach($value as $v){
- if(is_numeric($v)){
- $category = Mage::getModel('catalog/category')->load($v);
- $search[] = array('name'=>'Category','value'=>$category->getName());
- }
- }
- } else {
- if(is_numeric($value)){
- $category = Mage::getModel('catalog/category')->load($value);
- $search[] = array('name'=>'Category','value'=>$category->getName());
- }
- }
- }
- return $search;
- }
and
- 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);
- /* include category filtering */
- if(isset($_GET['category'])){
- $value = $this->getCatsFromSearchUrl($_GET['category']);
- if(is_array($value)){
- foreach($value as $v){ if(is_numeric($v)){ $this->_productCollection->addCategoryFilter(Mage::getModel('catalog/category')->load($v),true); } }
- } else { if(is_numeric($value)){ $this->_productCollection->addCategoryFilter(Mage::getModel('catalog/category')->load($value),true); } }
- }
- }
- return $this->_productCollection;
- }
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:
- 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);
- /* include category filtering */
- if(isset($_GET['category'])){
- $value = $this->getCatsFromSearchUrl($_GET['category']);
- if(is_array($value)){
- $this->_productCollection->joinField('category_id', 'catalog/category_product_index', 'category_id', 'product_id = entity_id', null, 'left')->addAttributeToFilter('category_id', array('in' => $value));
- }
- } else { if(is_numeric($value)){ $this->_productCollection->addCategoryFilter(Mage::getModel('catalog/category')->load($value),true); } }
- }
- }
- return $this->_productCollection;
- }
Note: the $value is an array with the category ids comma separated (1,2,4)


