Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 如何在Magento中对已加载的产品集合进行排序?_Sorting_Magento - Fatal编程技术网

Sorting 如何在Magento中对已加载的产品集合进行排序?

Sorting 如何在Magento中对已加载的产品集合进行排序?,sorting,magento,Sorting,Magento,我想对已由加载的产品集合进行排序 $_productCollection = $this->getLoadedProductCollection(); admin Magento中的默认排序是Style属性 我想先按款式、颜色、名字分类 我试过了 $_productCollection->setOrder(array('style', 'color','name'), asc); 而且 $_productCollection->addAttributeToSort('col

我想对已由加载的产品集合进行排序

$_productCollection = $this->getLoadedProductCollection();
admin Magento中的默认排序是Style属性

我想先按款式、颜色、名字分类

我试过了

$_productCollection->setOrder(array('style', 'color','name'), asc);
而且

$_productCollection->addAttributeToSort('color', Varien_Data_Collection::SORT_ORDER_ASC);
$_productCollection->addAttributeToSort('name', Varien_Data_Collection::SORT_ORDER_ASC);
但它不起作用


默认排序工作正常。有人能帮忙吗?

你可以这样做:

$_productCollection = $this->getLoadedProductCollection();

$_productCollection->clear();
$_productCollection->addAttributeToSort('color', Varien_Data_Collection::SORT_ORDER_ASC);

foreach ($_productCollection as $product) {
    // ...
}

通过这种方式,强制重新加载集合,然后应用自定义排序。

您还可以克隆加载的集合,然后根据需要修改查询

//当前集合的克隆。修改集合的更好方法

$_productCollection = clone $this->getLoadedProductCollection();
//取消cuurent COLLECTION并附加自定义筛选器

$_productCollection->clear()
               ->addAttributeToFilter('color', Varien_Data_Collection::SORT_ORDER_ASC)
               ->load();

你的回答很有魅力。谢谢你的解决方案。它救了我一天:)