Magento类别产品列表按畅销书选项排序


Magento Category Product List Sort By Bestseller Options

我正在尝试在类别产品列表的选择框中获得"最佳卖家"选项。

我已经扩展

class Mymodule_Catalog_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{
    protected function _construct()
    {
        parent::_construct();
    }
    public function getAvailableOrders()
    {
        $this->_availableOrder['bestseller'] = $this->__('Best Seller');
        return $this->_availableOrder;
    }

}

之后,我在选择框中获得了"最畅销"选项。但是,我不知道该怎么做。如有任何帮助,我们将不胜感激。

您也需要扩展setCollection():

public function getAvailableOrders()
{
    $this->_availableOrder['bestseller'] = $this->__('Best Seller');
    return $this->_availableOrder;
}

public function setCollection($collection)
{
    // ...
    if ($this->getCurrentOrder()) {
        if ($this->getCurrentOrder() == 'bestseller') {
            // add needed joins to collection here to get 'bestseller' column in collection
        }
        $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
    }
    return $this;
}

顺便说一句,你知道$this->__('Best Seller');是坏风格吗?正如您所知,$this->__()是当前模块数据助手__()方法的快捷方式。但是,如果有人将您的块扩展到另一个模块中,则该模块数据助手将用于将"畅销书"字符串翻译为其他语言。很明显,在他的模块中,他可能没有"畅销书"字符串的翻译。这就是为什么您必须始终使用Mage::helper('your_module/data')->__()进行翻译。