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();
你的回答很有魅力。谢谢你的解决方案。它救了我一天:)