Our blog

 

Magento Search Within Current Top Level Category

If you fancy having a select box allowing people to search within a top level category on your site directly from the quick search box, check out this little modification. This gives your search a bit of an Amazon feel. This idea could easily be extended if required.

You can drop this file in as a straight replacement to app/design/frontend/###/###/template/catalogsearch/form.mini.phtml

PHP:
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Academic Free License (AFL 3.0)
  8. * that is bundled with this package in the file LICENSE_AFL.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/afl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category   design_default
  22. * @package    Mage
  23. * @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
  24. * @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
  25. *
  26. *
  27. * @version Edmonds Commerce Quick Search with Top Level Categories
  28. *
  29. */
  30.  
  31. $category = Mage::getModel('catalog/category');
  32. if(is_object(Mage::registry('current_category'))){
  33.     $current_category_path=Mage::registry('current_category')->getPathIds();
  34. }else{
  35.     $current_category_path = array();
  36. }
  37. $category->load(Mage::app()->getStore()->getRootCategoryId());
  38. $children_string = $category->getChildren();
  39. $children = explode(',',$children_string);
  40. $extra_options='';
  41. foreach($children as $c){
  42.     $selected = (in_array($c, $current_category_path))?'SELECTED':'';
  43.     $extra_options.= '<option value="' . $c . '" ' . $selected . '>' . $category->load($c)->getName() . '</option>' . "\n";
  44. }
  45. ?>
  46. <form id="search_mini_form" action="<?php echo $this->helper('catalogSearch')->getResultUrl() ?>" method="get">
  47.     <fieldset>
  48.         <legend><?php echo $this->__('Search Site') ?></legend>
  49.         <div class="mini-search">
  50.             <input id="search" type="text" class="input-text" name="<?php echo $this->helper('catalogSearch')->getQueryParamName() ?>" value="<?php echo $this->helper('catalogSearch')->getEscapedQueryText() ?>" />
  51.             <select name="cat" id="cat" class="input-text">
  52.             <option value="">All Departments</option>
  53.             <?= $extra_options ?>
  54.            </select>
  55.             <input type="submit" value="Go" style="border: 1px solid #808080;" alt="<?php echo $this->__('Search') ?>" />
  56.             <div id="search_autocomplete" class="search-autocomplete"></div>
  57.             <script type="text/javascript">
  58.             //<![CDATA[
  59.                 var searchForm = new Varien.searchForm('search_mini_form', 'search', '<?php echo $this->__('search site...') ?>');
  60.                 searchForm.initAutocomplete('<?php echo $this->helper('catalogSearch')->getSuggestUrl() ?>', 'search_autocomplete');
  61.             //]]>
  62.             </script>
  63.         </div>
  64.     </fieldset>
  65. </form>

More Reading:

  • no matching posts found..
13 Comments

phpcook
November 18th, 2009

thanks :D

 

magento
December 1st, 2009

A Beauty! Thanks. How can I have the search option only display certain categories? Applying assigned IDs from admin. I guess using an array of category IDs will do the trick. Tried the "load" option, but nothing happened. Any tips would be appreciated. Thanks.

 

admin
December 1st, 2009

yes just edit the $children array to suit,

or if you want to blacklist certain categories you could just

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

 

magento
December 4th, 2009

Thanks. That helped a lot. Any tip of how I can access the second sub-categories. I played around with getStoreCategories() and seemed to be able to get only the first sub. Root Category > First Sub > Second Sub.

I changed Line 38
$children_string = $category->getChildren();
to
$children_string = $category->getStoreCategories();

Thanks in advance.

 

magentonews
December 4th, 2009

how about

PHP:
  1. if($this->getSubCategories($c)){
  2. foreach($this->getSubCategories($c) as $sc){
  3. ...
  4. }

 

magento
December 4th, 2009

@magentonews. Where do you exactly insert this code? I tried several logical permutations, but kept getting errors. Thanks.

 

magento
December 10th, 2009

I'll really appreciate it if anyone can solve this little dilemma, that would help a lot. I'm interested in accessing specific category block. The sub-sub-category. Root Category -> Sub-Category -> Sub-Sub-Category.

How can we implement if possible @magentonews code snippet? Any better alternative? Thanks.

 

admin
December 10th, 2009

presumably its along the lines of

PHP:
  1. if($this->getSubCategories($c)){
  2. foreach($this->getSubCategories($c) as $sc){
  3. foreach($this->getSubCategories($sc) as $ssc){
  4. ...
  5. }
  6. }
  7. }

 

magento
December 10th, 2009

Thanks admin. I feel like I'm twisting your arm here, either I can't see through the code or being sick with the cold for the last few days is obstructing my view.

Could you be more detailed of where to insert this code? Which part of the code above does it embed? Thanks a bunch. Tried few places but kept getting errors.

 

Nicolas
February 15th, 2010

How to sort categories to be in the same order as menu?

 

lynts
March 27th, 2010

so this is awesome, now what if i want to have 3 drop downs based on profile width and diameter then these would have the set demensions

 

admin
March 30th, 2010

pass :)

 

Nick
June 29th, 2010

Just tried this.

All I get is all departments. Can't see for the life of me why it's not working in V.1.3x

 

 

Leave a Reply