2

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?

  • I don't understand the question. Selects in magento are normally like this: <option value="INT_VALUE">STORE_SPECIFIC_TEXT_VALUE</option> – Fabian Blechschmidt Apr 2 '14 at 21:10
3

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

| improve this answer | |
  • Perfect, that's exactly what I was after – STW Apr 3 '14 at 17:26
  • Sorry, this makes no sense for me: if($arg_value == $option['label']) Because: You try to get the current label and say if(value1 == value1value2value...) So the "if" is senseless How do I get the current ID of a current Dropdown value? – TonkBerlin Jan 5 '15 at 17:43
  • The == operator is a comparison operator in PHP, it is not an assignment operator (which is a single = character). Given that information it should be clear that the if is not sensless at all. – Vinai Jan 7 '15 at 20:15
0

Here's a more concise approach to translating a text label into it's value of:

// Returns the value ID of the 'example' label for 'some_attribute'
Mage::getModel('catalog/product')->getResource()
  ->getAttribute('some_attribute')
  ->getSource()->getOptionId('example');

Bonus: To convert an integer value to a text label is a little easier:

Mage::getModel('catalog/product')
  ->setSomeAttribute(5)
  ->getAttributeText('some_attribute');

Both of those examples leverage the catalog/product model without actually loading a product. If you're working with a loaded product you can use that instance as well.

| improve this answer | |

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.