{"id":1074,"date":"2013-03-16T15:41:01","date_gmt":"2013-03-16T15:41:01","guid":{"rendered":"http:\/\/invisiblezero.net\/?p=548"},"modified":"2013-03-16T15:41:01","modified_gmt":"2013-03-16T15:41:01","slug":"magento-get-product-attributes-select-option-idlabelvalue","status":"publish","type":"post","link":"http:\/\/ndthanh.com\/magento-get-product-attributes-select-option-idlabelvalue\/","title":{"rendered":"Magento – Get product attribute\u2019s select option id\/label\/value"},"content":{"rendered":"
\nIf 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\u2019s value from label, label from value easily in Magento.
\nSuppose, you have an product attribute called \u201ccolor\u201d in Magento. You have the label (e.g. Red), and you want to find it\u2019s value. The below code will help you get the value for it.<\/p>\n
<\/p>\n
\n$productModel = Mage::getModel('catalog\/product');\n\n$attr = $productModel->getResource()->getAttribute("color");\n\nif ($attr->usesSource())\n\n{\n\necho $color_id = $attr->getSource()->getOptionId("Red");\n\n}\n<\/pre>\nNow suppose, you have the value (let\u2019s say 8, for Red) for your attribute, but want to get the label for it. Below code will help you to achive it.<\/p>\n
\n$productModel = Mage::getModel('catalog\/product');\n\n$attr = $productModel->getResource()->getAttribute("color");\n\nif ($attr->usesSource())\n\n{\n\necho $color_label = $attr->getSource()->getOptionText("8");\n\n}\n\n<\/pre>\nNote 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.<\/p>\n","protected":false},"excerpt":{"rendered":"
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…<\/p>\n