Searching product in different Category In Magento

Suppose You want to search a product in specific Category This code will help you. for this you just need to go  catalogSearch and paste this code in then you will show the result

* Magento
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE_AFL.txt.
* It is also available through the world-wide-web at this URL:
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to so we can send you a copy immediately.
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to for more information.
* @category   design_default
* @package    Mage
* @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (
* @license  Academic Free License (AFL 3.0)
* @version Edmonds Commerce Quick Search with Top Level Categories
$category = Mage::getModel('catalog/category');
$current_category_path = array();
$children_string = $category->getChildren();
$children = explode(',',$children_string);
foreach($children as $c){
$selected = (in_array($c, $current_category_path))?'SELECTED':'';
$extra_options.= '<option value="' . $c . '" ' . $selected . '>' . $category->load($c)->getName() . '</option>' . "\n";
<form id="search_mini_form" action="<?php echo $this->helper('catalogSearch')->getResultUrl() ?>" method="get">
<legend><?php echo $this->__('Search Site') ?></legend>
<input id="search" type="text" name="<?php echo $this->helper('catalogSearch')->getQueryParamName() ?>" value="<?php echo $this->helper('catalogSearch')->getEscapedQueryText() ?>" />
<select name="cat" id="cat">
<option value="">All Departments</option>
<?= $extra_options ?>
<input type="submit" value="Go" style="border: 1px solid #808080;" alt="<?php echo $this->__('Search') ?>" />
<div id="search_autocomplete"></div>
<script type="text/javascript">
var searchForm = new Varien.searchForm('search_mini_form', 'search', '<?php echo $this->__('search site...') ?>');
searchForm.initAutocomplete('<?php echo $this->helper('catalogSearch')->getSuggestUrl() ?>', 'search_autocomplete');

this above code is only for Displaying Current category and if someone want to search in specific category

then you need to change the code like

foreach($children as $c){
if(in_array($c, $exclude_array)){continue;}
$selected = (in_array($c, $current_category_path))?'SELECTED':'';
$extra_options.= '<option value="' . $c . '" ' . $selected . '>' . $category->load($c)->getName() . '</option>' . "\n";

on the otherhand if someone want to get all category and subcategory then

foreach($this->getSubCategories($c) as $sc){
foreach($this->getSubCategories($sc) as $ssc){

for more info visit mage-people

  1. Hi…This post is use full for me, but i want that if any one not fill the search text within the text box and he/she select just only category from drop down and click on the submit button it show the result from selected category. I mean the search text box filed should not be mandatory ….waiting for response

    • kathirvel
    • July 7th, 2012

    nice tutorial,conitue this type of work, thanks

  2. thanks a lot, but still it not displaying categories and subcategories in drop down

  3. asap help me sir

  4. Thanks Ahmed your code is working perfectly and helped me a lot…..

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: