Magento首先在list.phtml中展示产品“new_from_date”

时间:2013-12-07 20:39:19

标签: magento sorting collections

我尝试在list.phtml中首先显示产品“new_from_date”..

我尝试覆盖添加

的List.php块类
   $this->_productCollection = $layer->getProductCollection()->addAttributeToSort('news_from_date','desc');

但不行...... 有帮助吗? 感谢

1 个答案:

答案 0 :(得分:0)

我认为问题是Mage_Catalog_Block_Product_List块中的getSortBy魔术方法。并在

protected function _beforeToHtml()

您可能会看到该代码

if ($sort = $this->getSortBy()) {
    $toolbar->setDefaultOrder($sort);
}

...

$toolbar->setCollection($collection);

然后转到工具栏(Mage_Catalog_Block_Product_List_Toolbar)

public function setCollection($collection)
{
...
    if ($this->getCurrentOrder()) {
        $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
    }

我认为这是一个问题。您可以尝试覆盖块中的受保护函数_beforeToHtml()并更改

if ($sort = $this->getSortBy()) {
    $toolbar->setDefaultOrder($sort);
}

$collection->addAttributeToSort('news_from_date','desc');

或者可以观察'catalog_block_product_list_collection'并在那里改变排序。这些只是建议,但我可以直接在_beforeToHtml()和工具栏中看到问题。