问题
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!
回答1:
I've alredy been experienced with addAttributeToSort in a previous project: maybe this function doesn't works until today try with setOrder('columname') or try to update your magento to last version
回答2:
Working great. In Magento 1.6 + use setOrder('sort_order').
回答3:
$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);
回答4:
At the begining of app/design/frontend/default/default/template/manapro/filtercheckboxes/items.phtml add following code:
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.
回答5:
As it eav collection it load load join query to collection and using load function. 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();
来源:https://stackoverflow.com/questions/5068201/magento-sort-attribute-option-collection-by-position