Tuesday, 1 July 2014

Magento Get dropdown attribute’s option Id or Lable or Value

This tutorial will guide you on how to get drop down attribute value or label or id

Get attribute id using label.
$productModel = Mage::getModel('catalog/product');
$attr = $productModel->getResource()->getAttribute("color_group");
if ($attr->usesSource()) {
    echo $attr->getSource()->getOptionId("Green");
}


Get attribute label using id.
$productModel = Mage::getModel('catalog/product');
$attr = $productModel->getResource()->getAttribute("color_group");
if ($attr->usesSource()) {
    echo $attr->getSource()->getOptionText("723");
}



Get all options by attribute Code.
$attributeId = Mage::getResourceModel('eav/entity_attribute')
->getIdByCode('catalog_product','color_group');

if ($attributeId->usesSource()) {
    $options = $attribute->getSource()->getAllOptions(false);
}


Get all options by attribute Id.
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load(723);
if ($attribute->usesSource()) {
    $options = $attribute->getSource()->getAllOptions(false);
}


Best of luck

No comments:

Post a Comment