Magento – How to Get ID of Dropdown Attribute by Text Label

attributescollection-filteringdropdown-attributescript

I have a product attribute which is configured to be a dropdown with a fixed list of available values.

Now I need to write a script which will filter based on the selected value of the dropdown attribute. The filter needs to know the integer ID of the value, but the script input uses the text based label of the attribute.

How would I lookup the integer ID of the desired attribute value?

Best Answer

You can use something like this.

 $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', "your attribute_code");
 $arg_value = "your  text based label";
    foreach ( $attribute->getSource()->getAllOptions(true, true) as $option )
    {

        if($arg_value == $option['label'])
        {

            unset($attribute);
            echo $option['value'] ; 
        }
    }

This will give you Integer ID of your text based label

Related Topic