【问题标题】:Magento - Sorting product collectionMagento - 对产品集合进行排序
【发布时间】:2013-01-15 07:33:41
【问题描述】:

我有一个需要过滤的产品集合。为此,我重写了“Mage_Catalog_Block_Product_List”并具有以下代码:

$collection = parent::_getProductCollection();
$collection->addAttributeToFilter('language', array('in' => 
array('B','C','E')));
$collection->addAttributeToSort('language', 'DESC');

return $this->_productCollection; 

在前端以 E、C、B 的形式排序

如果我将属性设置为“ASC”,则排序的形式为 B、C、E

我需要的是指定 E、B、C 的顺序

有什么办法可以做到吗?

感谢您的帮助。

更新

我尝试了 fin_Set 并查看了您发送的链接。我试过代码:

 $collection = parent::_getProductCollection();
 $collection->addAttributeToFilter('language', 
 array('finset'=>'E','C,E,B'));

return $this->_productCollection;

但是,这仅显示设置为“E”的产品我也尝试了您提供的代码,但我收到了一个空的产品列表。

【问题讨论】:

    标签: magento sorting collections filter product


    【解决方案1】:

    试试这个:

    parent::_getProductCollection()
            ->addAttributeToFilter('language', array('finset'=>'B,C,E'));
    
    return $this->_productCollection;
    

    finset 映射到 MySQL 的 FIND_IN_SET

    【讨论】:

    • 请更新我的问题。我已经根据您提供的链接尝试了您的代码和更改。
    • 我说的不是array('finset'=>'E','C,E,B'),而是array('finset'=>'C,E,B'))。我认为它甚至不像你写的那样有效。
    猜你喜欢
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多