Magento快速搜索 - 首先显示给定类别的结果

时间:2012-05-10 11:19:05

标签: magento sorting collections magento-1.6

我希望能够进行正常的快速搜索,增加一项功能:来自特定类别的结果排在第一位。

到目前为止,我已在protected function _getProductCollection()修改了Mage_CatalogSearch_Block_Result,但这有效,但它忽略了我希望在两组结果中应用的自定义搜索结果(例如按价格或名称)。

我的附加代码是:

$initial = $this->_productCollection->getAllIds();
$this->_productCollection->addCategoryFilter(Mage::getModel('catalog/category')->load(3));

$newids = $this->_productCollection->getAllIds();//$this->_productCollection->getAllIds();

$merged_ids = array_merge($newids, $initial);
$merged_ids = array_unique($merged_ids);

$this->_productCollection->addCategoryFilter(Mage::getModel('catalog/category')->load(2));

$this->_productCollection->getSelect()->order("find_in_set(e.entity_id,'".implode(',',$merged_ids)."')"); 

其中cat 2是根类别。

那么,集合在哪里排序?如果我在那里移动它应该做我相信的技巧(?)。

1 个答案:

答案 0 :(得分:0)

它可能不适合每个人的情况,但添加以下内容对我有用:

$this->_productCollection->setOrder($this->getRequest()->getParam('order'), $this->getRequest()->getParam('dir'));

所以我最终获得了protected function _getProductCollection()

protected function _getProductCollection()
{
    if (is_null($this->_productCollection)) {
        $this->_productCollection = $this->getListBlock()->getLoadedProductCollection();
    }
    $this->_productCollection->setOrder($this->getRequest()->getParam('order'), $this->getRequest()->getParam('dir'));
    $initial = $this->_productCollection->getAllIds();
    $this->_productCollection->addCategoryFilter(Mage::getModel('catalog/category')->load(3));

    $newids = $this->_productCollection->getAllIds();//$this->_productCollection->getAllIds();

    $merged_ids = array_merge($newids, $initial);
    $merged_ids = array_unique($merged_ids);

    $this->_productCollection->addCategoryFilter(Mage::getModel('catalog/category')->load(2));

    $this->_productCollection->getSelect()->order("find_in_set(e.entity_id,'".implode(',',$merged_ids)."')"); 

    return $this->_productCollection;
}