【问题标题】:How do I filter searches by category in Magento?如何在 Magento 中按类别过滤搜索?
【发布时间】:2013-06-11 13:01:59
【问题描述】:

如何在 Magento form-mini 和高级搜索中按类别搜索?

【问题讨论】:

    标签: magento search categories product


    【解决方案1】:

    form-mini外的搜索使用get字符串如下:

    http://www.example.com/catalogsearch/result/?cat=120&q=wire
    

    如果您想将搜索限制在顶级类别中的项目,您可以在 form-mini 中添加单选按钮来完成此操作。

    <div class="search-radio">
        <input type="radio" name="cat" value="" />All
        <input type="radio" name="cat" value="80" />Category one
        <input type="radio" name="cat" value="120" />Category two
        <input type="radio" name="cat" value="660" />Category three
        <input type="radio" name="cat" value="1054" />Category four
    </div>
    

    高级搜索也适用于获取字符串,您可以通过包含如下链接在本地类别列表中添加品牌搜索(在产品属性中定义品牌)。要让高级搜索过滤类别需要修改

    http://www.example.com/catalogsearch/advanced/result/?brand=Fancy%20Brand&category=1305
    

    为了让高级搜索能够看到 GET 字符串上的类别,需要将以下过滤器添加到 CatalogSearch/Model/Advanced.php 中的 getProductCollection() 函数中

    /* include category filtering */
    if(isset($_GET['category']) && is_numeric($_GET['category'])) $this->_productCollection->addCategoryFilter(Mage::getModel('catalog/category')->load($_GET['category']),true);
    

    将类别下拉列表添加到高级搜索模板有点棘手,需要修改一个块:

    <!-- populate dropdown with all categories (useful for small store with limited product -->
            <!-- <li>
               <label for="category_search_field">Search by Category</label>
               <select name="category" id="category_search_field">
                   <option value="">-- Any Category --</option>
                   <?php foreach ($this->getStoreCategories() as $_category): ?>
                   <?php if($_category->hasChildren()): ?>
                   <option class="parent-cat" value="<?php echo $_category->getId(); ?>"><?php echo $_category->getName();?></option>
                   <?php foreach ($_category->getChildren() as $subcategory):
                   if($subcategory->getIsActive()) : ?>
                       <option value="<?php echo $subcategory->getId(); ?>"<?php echo ($this->getRequest()->getQuery('category') == $subcategory->getId() ? ' selected="selected"': "") ?>><?php echo $subcategory->getName(); ?></option>
                   <?php endif; endforeach; ?>
                   <?php elseif($_category->getIsActive()): ?>
                   <option value="<?php echo $_category->getId(); ?>"><?php echo $_category->getName();?></option>
                   <?php endif; ?>
                   <?php endforeach ?>
               </select>
            </li> -->
    
    <!-- limit dropdown to top level categories (more useful for larger stores with a lot of categories) -->
            <li>
               <label for="section_search_field">Search by Section</label>
               <select name="category" id="section_search_field">
                   <option value="">-- Any Section --</option>
                   <option value="80">Category one</option>
                   <option value="120">Category two</option>
                   <option value="660">Category three</option>
                   <option value="1054">Category four</option>
               </select>
            </li>
        </ul>
    

    注释掉的所有类别下拉菜单需要在CatalogSearch/Block/Advanced/Form.php 中添加getAttributeSelectElement() 函数,如下所示:

    /* Allow search by Store Categories */
    public function getStoreCategories()
    {
       $helper = Mage::helper('catalog/category');
       return $helper->getStoreCategories();
    }
    

    ================================================ =======================

    注意:上面发布的原始代码适用于 1.4.2.0 和可能的 1.5.1.0。受影响的功能在 1.6.2.0 及更高版本中发生了变化

    类别过滤测试需要添加到同一文件CatalogSearch/Model/Advanced.php(通过您的自定义模块覆盖)到以下函数:

    对于addFilters($values) 并在函数结束之前进行如下:

        }
    
        /* Add category to test */
        if (($allConditions) || (isset($values['category']) && is_numeric($values['category']))) {
            $this->getProductCollection()->addFieldsToFilter($allConditions);
        } else if (!$hasConditions) {
            Mage::throwException(Mage::helper('catalogsearch')->__('Please specify at least one search term.'));
        }
    
        return $this;
    }
    

    对于_addSearchCriteria($attribute, $value) 并在函数结束之前进行如下:

        $this->_searchCriterias[] = array('name' => $name, 'value' => $value);
    
        /* Display category filtering criteria */
        if(isset($_GET['category']) && is_numeric($_GET['category'])) {
            $category = Mage::getModel('catalog/category')->load($_GET['category']);
            $this->_searchCriterias[] = array('name'=>'Category','value'=>$category->getName());
        }
        /* End Display category filtering criteria */
    
        return $this;
    }
    

    原来提到的函数getProductCollection(),由于某种原因我包含在新的重写中:

    /**
     * Retrieve advanced search product collection
     *
     * @return Mage_CatalogSearch_Model_Resource_Advanced_Collection
     */
    public function getProductCollection(){
        if (is_null($this->_productCollection)) {
            $collection = $this->_engine->getAdvancedResultCollection();
            $this->prepareProductCollection($collection);
            if (!$collection) {
                return $collection;
            }
            $this->_productCollection = $collection;
        }
    
        return $this->_productCollection;
    }
    

    and for prepareProductCollection($collection) and 就在函数结束之前,如下所示:

        Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);
        /* Include category filtering */
        if(isset($_GET['category']) && is_numeric($_GET['category'])) {
            $collection->addCategoryFilter(Mage::getModel('catalog/category')->load($_GET['category']),true);
        }
        /* End Include category filtering */
        return $this;
    }
    

    【讨论】:

    • 请告诉在哪里添加您首先提到的行?在结尾或开头或中间或在某些陈述之后?我使用但不知何故执行停止显示空白屏幕。请澄清!
    • 显示错误:[Mon Jul 20 13:12:25 2015] [error] PHP Fatal error: Call to a member function addCategoryFilter() on a non-object in /var/www/html/mykidscare/app/code/core/Mage/CatalogSearch/Model/Advanced.php on line 303
    • 是的!那会勾起不好的回忆。发布的代码适用于 1.4.2.0。 1.6.2.0 及更高版本发生了变化,我添加了您在重写中需要进行的更改。必须更改三个功能,而不仅仅是一个。
    • 嗨 Fiasco 我做了一些研发并找到了答案。
    【解决方案2】:

    感谢您的研发Fiasco Labs

    我使用 Magento 1.9,我得到了按照 ma​​gento 1.9

    的解决方案

    只需找到文件:D:\xampp\htdocs\mykidscare\app\code\local\Mage\CatalogSearch\Model\Advanced.php 并找到 getProductCollection。在//Custom code////Custom code 之间设置我写的内容。

    我刚刚在 //cutom 代码中添加了我的代码。 Rest 是原始的 magento 代码。

    public function getProductCollection(){
        if (is_null($this->_productCollection)) {
            $collection = $this->_engine->getAdvancedResultCollection();
            //Custom code
            if(isset($_GET['category']) && is_numeric($_GET['category'])) {
                $collection = $this->_engine->getAdvancedResultCollection()->addCategoryFilter(Mage::getModel('catalog/category')->load($_GET['category']),true);
            }
            ////Custom code
            $this->prepareProductCollection($collection);
    
            if (!$collection) {
                return $collection;
            }
            $this->_productCollection = $collection;
        }
        return $this->_productCollection;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-03
      • 1970-01-01
      相关资源
      最近更新 更多