11/25/2011

How to add search by category in magento mini search


There is no free extension in magento commerce which will help you to search your store with particular category. I tried from myself and able to do, I am sharing my exprerience here with.

First create the drop down list of all category in form.mini.phtml

<select name="category" id="category_search_field">
<option value="">-- Any Category --</option>
<?php foreach ($catalog->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>

Now go to app/code/core/Mage/CatalogSearch/Helper and open the Data.php and add the below code

public function getStoreCategories()
{
$helper = Mage::helper('catalog/category');
return $helper->getStoreCategories();
}
public function getSelectedCategory()
{
$catid = (int)addslashes($_REQUEST['category']);
$cat="";
if($catid>1)
$cat = Mage::getModel('catalog/category')->load($catid);
return $cat;
}

Now go to app/code/core/Mage/CatalogSearch/Model and open the Layer.php

replace

public function prepareProductCollection($collection)
{
$collection
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addSearchFilter(Mage::helper('catalogsearch')->getQuery()->getQueryText())
->setStore(Mage::app()->getStore())
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addStoreFilter()
->addUrlRewrite();
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);
return $this;
}

with

public function prepareProductCollection($collection)
{
if(Mage::helper('catalogsearch')->getSelectedCategory()!="")
{
$collection
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addSearchFilter(Mage::helper('catalogsearch')->getQuery()->getQueryText())
->setStore(Mage::app()->getStore())
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addStoreFilter()
->addCategoryFilter(Mage::helper('catalogsearch')->getSelectedCategory())
->addUrlRewrite();
}
else
{
$collection
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addSearchFilter(Mage::helper('catalogsearch')->getQuery()->getQueryText())
->setStore(Mage::app()->getStore())
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addStoreFilter()
->addUrlRewrite();
}
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);

return $this;
}

save and Enjoy.

17 comments:

  1. Really its very useful for topic.Keep writing some more interesting topics like a magento development

    ReplyDelete
  2. Hii it's not working in my website :(
    i use magento 1.6
    the result just show blank product page...
    can u tell me another code.. thanks

    ReplyDelete
    Replies
    1. See what I am doing, I am just filtering the result by the below condition. I have tried with 1.6.0 It worked .
      addCategoryFilter(Mage::helper('catalogsearch') -> getSelectedCategory());

      Try from yourself , may be you missed something. Logically it is correct

      Delete
    2. hai i just follow this,it not working,can u help me,i worked in magento 1.6

      Delete
    3. Not sure if this helps your situation but an extension I've used for 1.6 is http://www.magentocommerce.com/magento-connect/improved-search-1399.html.
      I don't think it works for 1.4 though.
      Has a multiple category filter. Pretty cheap considering what it does.

      Delete
  3. Hello, is it possible to implement product search in all sub-categories within parent ? Thanks

    ReplyDelete
  4. Yes, trying to fix on 1.6, coming up blank page

    ReplyDelete
  5. how to store the search category on the search result page..
    On search result page, it automatically select the "All Category" Category whereas i required it should select the search specific category in dropdown

    ReplyDelete
  6. Fatal error: Call to a member function getStoreCategories() on a non-object in /app/design/frontend/base/default/template/catalogsearch/form.mini.phtml on line 45

    ReplyDelete
  7. Anonymous4/05/2013

    Use

    $catalogSearchHelper->getStoreCategories() as $_category instead of

    Instead of
    $_category->getChildren() as $subcategory

    in form.mini.phtml



    ReplyDelete
  8. Indeed, Abhishek Kumar. Make sure the variable which calls the catalogsearch helper (in form.mini.phtml) is correct. So if you declared the variable as $catalogSearchHelper, then use the line:

    getStoreCategories() as $_category): ?>

    ReplyDelete
  9. Hey,

    Thanks for the code saved my time and achieved the desired result :)

    ReplyDelete
  10. anyone have this working in 1.7.2 ?

    I get a blank page after the drop down

    ReplyDelete
  11. yes same for me in 1.7.2

    ReplyDelete
  12. It's very helpful. Thank you very much.

    ReplyDelete