Adding search by Category feature to Magento Advanced search and catlog search in Product list view

I recently had to add a search form in the Magento product list view and I found the following Wiki article:

http://www.magentocommerce.com/wiki/how-to/how_to_add_search_by_category_to_advanced_search

This article would work 100% perfect, however the steps given in article is not really upgrade safe.

There were hence two options either to include these steps in a seperate magento module, or to follow the steps I have given here.
This is not really a perfect practice, however, if we are not aware of writing a magento module then this can be a very quick alternative.
This step also protects us from our work being over-written when we make a magento upgrade.

Step 1:
Create a new folder called “Mage” inside app/code/local

Now we have anew folder app/code/local/Mage

Step 2:
If I see the steps given in Magento wiki, we notice that the they are extending the features of the CatalogSearch module that is located in the namespace Mage,
So just go to “app/code/core/Mage” and copy the entire folder named “CatalogSearch” into app/code/local/Mage

Now we have the entire “CatalogSearch” module located in app/code/local/Mage/CatalogSearch

Step 3:
Now all we need to do is follow the exact steps given in the wiki article, however make sure to edit files located in app/code/local/Mage/CatalogSearch only.

Link to the wiki: http://www.magentocommerce.com/wiki/how-to/how_to_add_search_by_category_to_advanced_search

How to add catalog search in Magento product list view or the Search by category feature in Magento product list view.

This simply involves adding a search form in prodcut list view we however need to ensure that the category id is passed to the advanced search form using a hidden input.

Add the following code to

“app\design\frontend\YOURPACKAGENAME\default\template\catalog\product\list\toolbar.phtml”
OR
“app\design\frontend\YOURPACKAGENAME\default\template\catalog\product\list.phtml”

<?php if( !isset($_GET[‘category’]) ): ?>
<form action=”<?echo $this->getUrl(‘catalogsearch/advanced/result’)?>” method=”get” id=”category_search_form”>
<?php

if ($this->helper(‘catalog/data’)->getCategory()) {
$_ccat = $this->helper(‘catalog/data’)->getCategory();
} else {
$_ccats = $this->helper(‘catalog/data’)->getProduct()->getCategoryIds();
$_ccat = Mage::getModel(‘catalog/category’)->load($_ccats[0]);
};
$_ccatID = $_ccat->getId();
?>
<input type=”hidden” id=”category_search_field” name=”category” value=”<?php echo $_ccatID ?>” />
<div class=”left”>
<input name=”name” id=”searchcat” value=”<?php echo $this->__(‘Search Category’) ?>” title=”Name” class=”input-text ” type=”text” />
<input type=”image” alt=”<?php echo $this->__(‘Search’) ?>” src=”<?php echo $this->getSkinUrl(images/ico_s_search.png’)?>” class=”cw_buttonicon” />
</div>
</form>
<script type=”text/javascript”>
//<![CDATA[
var searchForm = new Varien.searchForm(‘category_search_form’, ‘searchcat’, ‘<?php echo $this->__(‘Search Category’) ?>’);
searchForm.initAutocomplete(‘<?php echo $this->helper(‘catalogSearch’)->getSuggestUrl() ?>’, ‘search_autocomplete’);
//]]>
</script>
<?php endif ?>

Note:
1.The first if statement <?php if( !isset($_GET[‘category’]) ): ?> makes sure that we will not have two forms in the advanced search result page.
You should retain this if statement because when we are in advanced search we will no longer be in the previously selected category!

2.
You may need to change the path of search image.

3. I have also included the autocomplete feature, so be careful if you are changing the name or ID attribute of the form or the input tags.

Hope this article would be useful, kindly do not forget to write your comments here. Thank you.