
如上图的搜索框是可以限定在某个分类下进行搜索的。我开始以为要给Magento搜索功能加个过滤条件才可以实现如上的功能,其实不然。
我们在Magento官网的demo(http://demo.magentocommerce.com/)中搜下 ‘HTC’,来看下Magento的搜索结果页Url如下:
http://demo.magentocommerce.com/catalogsearch/result/?q=HTC
而点击左侧的,按分类进行搜索后,Url变为:
http://demo.magentocommerce.com/catalogsearch/result/index/?cat=13&q=HTC
我们可以看到,多了个分类就是多了‘cat=13’参数,其中‘cat’代表分类,‘13’代表分类的ID。
看下Magento的搜索的<form method=”get”….. ,好吧,我们还是不要去管什么过滤和程序了,直接去改掉模板文件吧。只要提交的参数中有cat参数和代表分类ID的值就好了。
如果简单来实现,可以静态的在<form>中加上你需要被筛选的分类下拉菜单,这个适合各种新手。可以动态的获取分类当然是最好的。
Search搜索的模板文件位置,开启模板路径看下。
如下代码示例,为获得一级分类下拉菜单作为搜索条件:
$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 class="parent-cat" value="' . $c . '" ' . $selected . '>' . $category->load($c)->getName() . '</option>' . "\n"; } ?> <form id="search_mini_form" action="<?php echo $this->helper('catalogSearch')->getResultUrl() ?>" method="get"> <fieldset> <table height="33" cellspacing="0" cellpadding="0" border="0" width="472"> <tbody> <tr> <td id="nav-search-field" align="left" width="260"> <input id="search" type="text" class="input-live-search" name="<?php echo $this->helper('catalogSearch')->getQueryParamName() ?>" value="<?php echo $this->helper('catalogSearch')->getEscapedQueryText() ?>" /></td> <td class="category_search_select" width="90"> <select name="cat" id="cat" > <option value=""><?php echo $this->__('All Categories') ?></option> <?php echo $extra_options; ?> </select> </td> <td align="center" width="45" style="text-align:center"> <input type="image" style="margin-top:1px;" src="<?php echo $this->getSkinUrl('images/nav_searchbar_submit.gif') ?>" alt="search" /> </td> <td align="center" width:="77"> <a href="<?php echo $this->getUrl('catalogsearch/advanced') ?>"><?php echo $this->__('Advanced') ?></a> </td> <div id="search_autocomplete" class="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> </tr> </tbody> </table> </fieldset> </form>
转载表明出处:www.hellokeykey.com
好东西啊!怎么下载这段代码呢?
复制黏贴,google下应该有不少英文文章介绍这个,似乎还有插件