Get Product attribute’s select option Id or Lable or Value in Magento

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.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. Purple), and you want to find it’s value. The below code will help you get the value for it.

$_product = Mage::getModel('catalog/product');
$attr = $_product->getResource()->getAttribute("color");
if ($attr->usesSource()) {
echo $color_id = $attr->getSource()->getOptionId("Purple");
}

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

$_product = Mage::getModel('catalog/product');
$attr = $_product->getResource()->getAttribute("color");
if ($attr->usesSource()) {
echo $color_label = $attr->getSource()->getOptionText("10");
}

Leave a comment

3 Comments

  1. Your method of explaining all in this paragraph Get Product attribute’s select option Id or Lable or Value in Magento | Pradeep Sanku is genuinely good, all be capable of effortlessly know it, Thanks a lot.

    Reply
  2. I have read so many Get Product attribute’s select option Id or Lable or Value in Magento | Pradeep Sanku regarding the blogger lovers however this article is really a nice paragraph, keep it up.

    Reply
  3. Reblogged this on huztechbuzz.

    Reply

Leave a comment