[英]magento sort attribute option collection by position?
Greetings, 问候,
I am trying to sort an array of attribute option values by their "position" as entered in the manage attributes panel. 我正在尝试按在“管理属性”面板中输入的属性选项值的“位置”对属性数组进行排序。 I seem to have tried everything, does anyone know how this is possible? 我似乎已经尝试了所有方法,有人知道这怎么可能吗?
I thought for sure this would work: 我以为这肯定行得通:
$_collection = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setStoreFilter(0)
->setAttributeFilter($_productAttribute->getId())
->addAttributeToSort('position')
->load();
But it didn't. 但事实并非如此。 Any help would be greatly appreciated! 任何帮助将不胜感激!
我在以前的项目中对addAttributeToSort经验丰富:也许直到今天尝试使用setOrder('columname')或尝试将您的magento更新为最新版本,该功能才有效
$attribute = Mage::getModel('eav/entity_attribute')->load( $code, 'attribute_code');
$option_col = Mage::getResourceModel( 'eav/entity_attribute_option_collection')
->setAttributeFilter( $attribute->getId() )
->setStoreFilter()
->setPositionOrder( 'ASC' );
$option_col->getSelect()->order('main_table.sort_order '.$orderby);
At the begining of app/design/frontend/default/default/template/manapro/filtercheckboxes/items.phtml add following code: 在app / design / frontend / default / default / template / manapro / filtercheckboxes / items.phtml的开头添加以下代码:
function cmp($a, $b){
if ($a == $b)
return 0;
return ($a['position'] < $b['position']) ? -1 : 1;
}
$array = $this->getItems();
usort($array, "cmp");
And replace $this->getItems() with $array in foreach loop. 并在foreach循环中将$ this-> getItems()替换为$ array。
As it eav collection it load load join query to collection and using load function. 在进行eav收集时,将加载联接查询加载到收集中并使用加载功能。 so if you add order after 因此,如果您在之后添加订单
Mage::getResourceModel('eav/entity_attribute_option_collection')
just like: 就像:
$_collection = Mage::getResourceModel('eav/entity_attribute_option_collection')->getSelect()->order('main_table.sort_order '.$orderby);
$_collection->setStoreFilter(0)
->setAttributeFilter($_productAttribute->getId())
->load();
Working great. 工作很棒。 In Magento 1.6 + use setOrder('sort_order')
. 在Magento 1.6及setOrder('sort_order')
使用setOrder('sort_order')
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.