1

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.

3
  • So, you start a bounty, people waste their time to help, and then you don't even validate the answers... Not fair, man Commented Mar 16, 2016 at 7:53
  • Sorry, I'll do it now :)
    – Bizboss
    Commented Mar 16, 2016 at 10:32
  • Nice ;) Regards, man Commented Mar 16, 2016 at 10:34

2 Answers 2

5
+25

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

1
  • sorry I have tried it and it don't work ! :(
    – Bizboss
    Commented Mar 31, 2016 at 10:22
2

Raul was almost correct, but his answer returns a collection, not a simple object. For a specific attribute option it would be:

$option = Mage::getResourceModel('eav/entity_attribute_option_collection')
          ->setIdFilter($attrOptionId)
          ->getFirstItem();

Or to get the sort order (position) of the option directly:

$position = Mage::getResourceModel('eav/entity_attribute_option_collection')
            ->setIdFilter($attrOptionId)
            ->getFirstItem()
            ->getSortOrder();
1
  • Thumbs up for you ;) Commented Apr 23, 2018 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.