如何在Magento中按类别过滤搜索?

use*_*606 1 search product magento categories

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

Fia*_*abs 8

从form-mini中搜索使用get字符串如下:

http://www.example.com/catalogsearch/result/?cat=120&q=wire
Run Code Online (Sandbox Code Playgroud)

如果您想要将搜索限制为顶级类别中的项目,可以在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>
Run Code Online (Sandbox Code Playgroud)

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

http://www.example.com/catalogsearch/advanced/result/?brand=Fancy%20Brand&category=1305
Run Code Online (Sandbox Code Playgroud)

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

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

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

<!-- 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>
Run Code Online (Sandbox Code Playgroud)

注释掉所有类别下拉列表需要在getAttributeSelectElement()函数中添加CatalogSearch/Block/Advanced/Form.php如下内容:

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

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

注意:上面的原始代码发布是1.4.2.0,可能是1.5.1.0.受影响的功能在1.6.2.0及更高版本中已更改

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

For 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;
}
Run Code Online (Sandbox Code Playgroud)

For _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;
}
Run Code Online (Sandbox Code Playgroud)

由于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;
}
Run Code Online (Sandbox Code Playgroud)

并且prepareProductCollection($collection)正好在函数结束之前进行,如下所示:

    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;
}
Run Code Online (Sandbox Code Playgroud)