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

时间:2013-06-11 13:01:59

标签: magento search categories product

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

2 个答案:

答案 0 :(得分:8)

从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字符串上查看类别,需要将以下过滤器添加到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);

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

<!-- 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>

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

/* 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;
}

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;
}

答案 1 :(得分:2)

感谢您的R&amp; D //*[local-name()='feed']/*[local-name()='entry']/*[local-name()='content'] /*[local-name()='properties']/*[local-name()='ID' and @m:type='Edm.Int32']

我使用 Magento 1.9 ,按照 magento 1.9

获得解决方案

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

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

////Custom code