6

For a Magento Dropdown Attribute, it is possible to get an Attribute option Id by Attribute admin value?

7

Suppose, you have an product attribute called "color" in Magento. You have the attribut admin value (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->setStoreId(0)->getSource()->getOptionId("Purple");
}
2
  • Thanks for help, but it works only with the Default Store View option value, when I try with the admin value I get NULL
    – Bizboss
    Jul 8 '15 at 12:59
  • This does not seem to work always. Somehow we now have an attr option admin value that just DOES NOT return an id.
    – snh_nl
    May 27 '19 at 17:31
5

If you have the admin value :

echo $color_id = $attr->setStoreId(0)->getSource()->getOptionId("purple");

1
  • This should be the accepted answer, as it is the only one that actually answers the question that was actually asked. Worked for me.
    – Louis B.
    May 31 '18 at 12:09
3

i was face same problem the solution work for me. because the frontend store values were not added. this code work for me.

$attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product','color');
    $attr = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
    if ($attr->usesSource()) {

    $attributeOptions = $attr ->getSource()->getAllOptions();

    foreach ($attributeOptions as $option) {
        if($option['label']=='Purple'){
           echo $curattributeid=$option['value'];
        }
      }

    }
3

Code below checks if the attribute already have an option then return its id other wise add new option and return its id

    function addAttributeOption($attributeCode, $argValue) {
        $attribute = Mage::getModel('eav/config')
            ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);


        if ($attribute->usesSource()) {

           $id = $attribute->getSource()->getOptionId($argValue);
           if ($id) 
                return $id;
        }

         $value = array('value' => array(
            'option' => array(
                    ucfirst($argValue),
                    ucfirst($argValue)
                )
            )
        );

        $attribute->setData('option', $value);
        $attribute->save();

        //getting id of newly inserted option
        $attribute = Mage::getModel('eav/config')
            ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);
        if ($attribute->usesSource()) {
            return $attribute->getSource()->getOptionId($argValue);
        }
   }

Use it as

$this->addAttributeOption("uniform_type", "leotard");
1

Yes you can get the option ID of an attribute by the admin value. You can use the eav/entity_attribute_option_collection to achieve it:

$attributeCode = 'attr_code';
$attributeAdminValue = 'something';

$attribute = Mage::getModel('eav/entity_attribute')->loadByCode(Mage_Catalog_Model_Product::ENTITY, $attributeCode);
$collection = Mage::getResourceModel('eav/entity_attribute_option_collection')
    ->setAttributeFilter($attribute->getId())
    ->setStoreFilter(Mage_Core_Model_App::ADMIN_STORE_ID)
    ->addFieldToFilter('tdv.value', $attributeAdminValue);

if ($collection->getSize() > 0) {
    echo $collection->getFirstItem()->getId();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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