如何在标题搜索Magento中显示带有类别过滤器的产品

时间:2015-09-28 13:21:22

标签: javascript ajax forms magento magento-1.9

我正在尝试在标题auto suggest

中显示基于类别的search结果

form.mini.phtml

<form id="search_mini_form" action="<?php echo $catalogSearchHelper->getResultUrl() ?>" method="get">   
     <select id="select-category" title="Select your category" name="cat">
        <option value="0" selected>Select category</option>
        <?php
           foreach ($categoryCollection as $category) { ?>
            <option value="<?php echo $category->getId(); ?>"><?php echo $category->getName(); ?></option>
         <?php } ?>
     </select>
     <div class="input-box">
        <label for="search"><?php echo $this->__('Search:') ?></label>
        <input id="search" type="search" name="<?php echo $catalogSearchHelper->getQueryParamName() ?>" value="<?php echo $catalogSearchHelper->getEscapedQueryText() ?>" class="input-text required-entry" maxlength="<?php echo $catalogSearchHelper->getMaxQueryLength();?>" placeholder="<?php echo $this->__('Search entire store here...') ?>" />
         <button type="submit" title="<?php echo $this->__('Search') ?>" class="button search-button"><span><span><?php echo $this->__('Search') ?></span></span></button>
    </div>    
    <div id="search_autocomplete" class="search-autocomplete"></div>
    <script type="text/javascript">
      //<![CDATA[
         var searchForm = new Varien.searchForm('search_mini_form', 'search', '');
         searchForm.initAutocomplete('<?php echo $catalogSearchHelper->getSuggestUrl() ?>', 'search_autocomplete');
        //]]>
   </script>
</form>

enter image description here

在此表单中,当我输入输入框时,ajax请求会像

一样传递
http://127.0.0.1/site/catalogsearch/ajax/suggest/?q=bla

但选定的cat值未包含在此网址中,如何在Cat="bla"网址中加入ajax/suggest

http://127.0.0.1/site/catalogsearch/ajax/suggest/?q=bla&cat=bla

谢谢!

1 个答案:

答案 0 :(得分:0)

如果您已在任何类别页面上实施此搜索功能,则可以从控制器中获取类别ID以进行过滤:

Mage::registry('current_category')->getId();

如果您仍希望编辑除类别页面以外的页面的ajax调用,可以在以下位置编辑它:

magento / js / varien / js.js(debug initAutocomplete function)