`
coolsooner
  • 浏览: 1311525 次
文章分类
社区版块
存档分类
最新评论

magento--搜索功能添加分类选项!

 
阅读更多

magento的搜索是没有分类条件的,我们可以给他加上这个条件,在综合站搜索比较精准一些!

当然,magento的搜索设置也要设置好,太模糊的搜索会造成搜索的不精准。

下面是代码,覆盖文件template/catalogsearch/form.mini.phtml文件即可~!!!!


  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>


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics