【问题标题】:Magento Quick Search - showing results from given category firstMagento 快速搜索 - 首先显示给定类别的结果
【发布时间】:2012-05-18 23:03:13
【问题描述】:

我希望进行正常的快速搜索,并增加一个功能:给定类别的结果排在第一位。

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

我的附加代码是:

$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 是根类别。

那么,排序的集合在哪里?如果我把它移到那里,我认为应该可以解决问题(?)。

【问题讨论】:

  • 另一种方法是从Toolbar.php 访问getCurrentOrder()getCurrentDirection()。我该怎么做?

标签: magento sorting collections magento-1.6


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-28
    • 1970-01-01
    • 2017-01-22
    • 2019-02-25
    相关资源
    最近更新 更多