Magento – Get Attribute Option Position from Option value

attribute-optionsattributesmagento-1.7

I have a dropdown attribute with many options.

In cart.phtml I already get the attribute value for each item

Mage::getModel('catalog/product')->load($_item->getProduct()->getId())->getMyAttribute();

In addition to that I want to get the option position.

How can I do that without loading all Attribute options.

Best Answer

Assuming you have $optionId ( that's the value you get when calling $product->getData('your_attribute') ), you can get the position (sort_order) this way

$options = Mage::getResourceModel('eav/entity_attribute_option_collection')
    ->setIdFilter($optionId);

This will return a simple object, something like...

Array
(
    [option_id] => 624
    [attribute_id] => 298
    [sort_order] => 0
)

Check Mage_Eav_Model_Resource_Entity_Attribute_Option_Collection for more details