对于magento默认的搜索,是不带有分类选项的,当你的网店是一个综合站,每一个分类下面放的是不同类型的产品,那么为了更加精确地搜索,需要增加一个分类选项,毕竟,你不喜欢在搜索球衣的时候出来避孕套吧?呵呵

既然这样,加上后客户体验还能增加,就让我们加上magento分类选项吧!!

  1. <?php
  2. $category = Mage::getModel('catalog/category');
  3. if(is_object(Mage::registry('current_category'))){
  4.     $current_category_path=Mage::registry('current_category')->getPathIds();
  5. }else{
  6.     $current_category_path = array();
  7. }
  8. $category->load(Mage::app()->getStore()->getRootCategoryId());
  9. $children_string = $category->getChildren();
  10. $children = explode(',',$children_string);
  11. $extra_options='';
  12. foreach($children as $c){
  13.     $selected = (in_array($c, $current_category_path))?'SELECTED':'';
  14.     $extra_options.= '<option value="' . $c . '" ' . $selected . '>' . $category->load($c)->getName() . '</option>' . "\n";
  15. }
  16. ?>
  17. <form id="search_mini_form" action="<?php echo $this->helper('catalogSearch')->getResultUrl() ?>" method="get">
  18.     <fieldset>
  19.         <legend><?php echo $this->__('Search Site') ?></legend>
  20.         <div class="mini-search">
  21.             <input id="search" type="text" class="input-text" name="<?php echo $this->helper('catalogSearch')->getQueryParamName() ?>" value="<?php echo $this->helper('catalogSearch')->getEscapedQueryText() ?>" />
  22.             <select name="cat" id="cat" class="input-text">
  23.             <option value="">All Departments</option>
  24.             <?= $extra_options ?>
  25.            </select>
  26.             <input type="submit" value="Go" style="border: 1px solid #808080;" alt="<?php echo $this->__('Search') ?>" />
  27.             <div id="search_autocomplete" class="search-autocomplete"></div>
  28.             <script type="text/javascript">
  29.             //<![CDATA[
  30.                 var searchForm = new Varien.searchForm('search_mini_form', 'search', '<?php echo $this->__('search site...') ?>');
  31.                 searchForm.initAutocomplete('<?php echo $this->helper('catalogSearch')->getSuggestUrl() ?>', 'search_autocomplete');
  32.             //]]>
  33.             </script>
  34.         </div>
  35.     </fieldset>
  36. </form>

 

在magento相应的模板中覆盖template/catalogsearch/form.mini.phtml文件即可~!!!!,然后刷新缓存,编译,你会发现搜索的旁边出来一个分类选项!