user2474606
user2474606

Reputation: 79

How do I filter searches by category in Magento?

How do I search by category in Magento form-mini and also advanced search?

Upvotes: 1

Views: 15905

Answers (2)

Pratik Joshi
Pratik Joshi

Reputation: 11693

Thanks for your R&D Fiasco Labs.

I use Magento 1.9 and I got solution as per magento 1.9

Just find file : D:\xampp\htdocs\mykidscare\app\code\local\Mage\CatalogSearch\Model\Advanced.php and find getProductCollection. Set what i have written in between //Custom code and ////Custom code.

I just added my code in //cutom code. Rest is original magento code.

public function getProductCollection(){
    if (is_null($this->_productCollection)) {
        $collection = $this->_engine->getAdvancedResultCollection();
        //Custom code
        if(isset($_GET['category']) && is_numeric($_GET['category'])) {
            $collection = $this->_engine->getAdvancedResultCollection()->addCategoryFilter(Mage::getModel('catalog/category')->load($_GET['category']),true);
        }
        ////Custom code
        $this->prepareProductCollection($collection);

        if (!$collection) {
            return $collection;
        }
        $this->_productCollection = $collection;
    }
    return $this->_productCollection;
}

Upvotes: 1

Fiasco Labs
Fiasco Labs

Reputation: 6457

The search out of form-mini uses a get string as follows:

http://www.example.com/catalogsearch/result/?cat=120&q=wire

If you want to do something like limit your search to items in top-level categories, you could add radio buttons to the form-mini to accomplish this.

<div class="search-radio">
    <input type="radio" name="cat" value="" />All
    <input type="radio" name="cat" value="80" />Category one
    <input type="radio" name="cat" value="120" />Category two
    <input type="radio" name="cat" value="660" />Category three
    <input type="radio" name="cat" value="1054" />Category four
</div>

Advanced search also works with a get string, you can add brand searches in the local category list by including a link as follows (define brand in a product attribute). To get Advanced Search to filter on category requires a modification

http://www.example.com/catalogsearch/advanced/result/?brand=Fancy%20Brand&category=1305

For the advanced search to be able to see categories on the GET string, the following filter needs to be added to the getProductCollection() function in CatalogSearch/Model/Advanced.php

/* include category filtering */
if(isset($_GET['category']) && is_numeric($_GET['category'])) $this->_productCollection->addCategoryFilter(Mage::getModel('catalog/category')->load($_GET['category']),true);

Adding a category dropdown to the advanced search template is a little more tricky and requires modifying a block:

<!-- populate dropdown with all categories (useful for small store with limited product -->
        <!-- <li>
           <label for="category_search_field">Search by Category</label>
           <select name="category" 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 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> -->

<!-- limit dropdown to top level categories (more useful for larger stores with a lot of categories) -->
        <li>
           <label for="section_search_field">Search by Section</label>
           <select name="category" id="section_search_field">
               <option value="">-- Any Section --</option>
               <option value="80">Category one</option>
               <option value="120">Category two</option>
               <option value="660">Category three</option>
               <option value="1054">Category four</option>
           </select>
        </li>
    </ul>

The commented out all categories dropdown requires an addition to the getAttributeSelectElement() function in CatalogSearch/Block/Advanced/Form.php as follows:

/* Allow search by Store Categories */
public function getStoreCategories()
{
   $helper = Mage::helper('catalog/category');
   return $helper->getStoreCategories();
}

=====================================================================

NOTE: The original code posting above was for 1.4.2.0 and possibly 1.5.1.0. The affected functions have changed in 1.6.2.0 and later

The category filter tests need to be added to the same file CatalogSearch/Model/Advanced.php (via your custom module overwrite) to the following functions:

For addFilters($values) and goes just before the end of the function as follows:

    }

    /* Add category to test */
    if (($allConditions) || (isset($values['category']) && is_numeric($values['category']))) {
        $this->getProductCollection()->addFieldsToFilter($allConditions);
    } else if (!$hasConditions) {
        Mage::throwException(Mage::helper('catalogsearch')->__('Please specify at least one search term.'));
    }

    return $this;
}

For _addSearchCriteria($attribute, $value) and goes just before the end of the function as follows:

    $this->_searchCriterias[] = array('name' => $name, 'value' => $value);

    /* Display category filtering criteria */
    if(isset($_GET['category']) && is_numeric($_GET['category'])) {
        $category = Mage::getModel('catalog/category')->load($_GET['category']);
        $this->_searchCriterias[] = array('name'=>'Category','value'=>$category->getName());
    }
    /* End Display category filtering criteria */

    return $this;
}

The original mentioned function getProductCollection() which I included in the new rewrite for some reason:

/**
 * Retrieve advanced search product collection
 *
 * @return Mage_CatalogSearch_Model_Resource_Advanced_Collection
 */
public function getProductCollection(){
    if (is_null($this->_productCollection)) {
        $collection = $this->_engine->getAdvancedResultCollection();
        $this->prepareProductCollection($collection);
        if (!$collection) {
            return $collection;
        }
        $this->_productCollection = $collection;
    }

    return $this->_productCollection;
}

and for prepareProductCollection($collection) and goes just before the end of the function as follows:

    Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);
    /* Include category filtering */
    if(isset($_GET['category']) && is_numeric($_GET['category'])) {
        $collection->addCategoryFilter(Mage::getModel('catalog/category')->load($_GET['category']),true);
    }
    /* End Include category filtering */
    return $this;
}

Upvotes: 8

Related Questions