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 form.mini.phtml then you will show the result


<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* 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:
* http://opensource.org/licenses/afl-3.0.php
* 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 license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* 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 http://www.magentocommerce.com for more information.
*
* @category   design_default
* @package    Mage
* @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
* @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*
*
* @version Edmonds Commerce Quick Search with Top Level Categories
*
*/
$category = Mage::getModel('catalog/category');
if(is_object(Mage::registry('current_category'))){
$current_category_path=Mage::registry('current_category')->getPathIds();
}else{
$current_category_path = array();
}
$category->load(Mage::app()->getStore()->getRootCategoryId());
$children_string = $category->getChildren();
$children = explode(',',$children_string);
$extra_options='';
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">
<fieldset>
<legend><?php echo $this->__('Search Site') ?></legend>
<div>
<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 ?>
</select>
<input type="submit" value="Go" style="border: 1px solid #808080;" alt="<?php echo $this->__('Search') ?>" />
<div id="search_autocomplete"></div>
<script type="text/javascript">
//<![CDATA[
var searchForm = new Varien.searchForm('search_mini_form', 'search', '<?php echo $this->__('search site...') ?>');
searchForm.initAutocomplete('<?php echo $this->helper('catalogSearch')->getSuggestUrl() ?>', 'search_autocomplete');
//]]>
</script>
</div>
</fieldset>
</form>

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


$exclude_array=array(1,2,3);
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


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

for more info visit mage-people

Advertisements
  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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: