1

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

4

2 回答 2

8

form-mini 之外的搜索使用如下获取字符串:

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(通过您的自定义模块覆盖)到以下功能:

For addFilters($values)and 就在函数结束之前,如下所示:

    }

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

For _addSearchCriteria($attribute, $value)and 就在函数结束之前,如下所示:

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

和 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;
}
于 2013-06-11T16:54:34.440 回答
1

感谢您的研发Fiasco Labs

我使用Magento 1.9,我得到了根据magento 1.9的解决方案

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

我刚刚在 //cutom 代码中添加了我的代码。休息是原始的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;
}
于 2015-07-21T05:50:52.953 回答