Magento – Get product attribute’s select option id/label/value

magento-checklist_mini
If you have a select dropdown for any product attribute, to get the value from label or vice versa is always needed in order to display or get value for further processing, etc. Every now and then you will require this values while working on product attributes. There are many ways you can achieve it but the best, in terms of performance and simplicity is what I will tell you here. Get product attribute’s value from label, label from value easily in Magento.
Suppose, you have an product attribute called “color” in Magento. You have the label (e.g. Red), and you want to find it’s value. The below code will help you get the value for it.

$productModel = Mage::getModel('catalog/product');

$attr = $productModel->getResource()->getAttribute("color");

if ($attr->usesSource())

{

echo $color_id = $attr->getSource()->getOptionId("Red");

}

Now suppose, you have the value (let’s say 8, for Red) for your attribute, but want to get the label for it. Below code will help you to achive it.

$productModel = Mage::getModel('catalog/product');

$attr = $productModel->getResource()->getAttribute("color");

if ($attr->usesSource())

{

echo $color_label = $attr->getSource()->getOptionText("8");

}

Note that the only thing changed in both is, getOptionId() and getOptionText. getOptionId() will accept label and give you value, while getOptionText() will accept value and give you label.