Magento – Set dropdown value using text value

eavimportmagento-1.9productproduct-attribute

I have a small import script that will import product to Magento 1.

It works well but there is something not work.If a product attribute happen to be a select type attribute the value will not save. I can determine this cause through eav system and magento would like to have the option id and not text value.

I have:

$product->setColour('red')->save();

But magento want:

$product->setColour(22)->save();

Is a method in magento that will accept the text value and correctly set the option id in the background for me?

Best Answer

This is untested but should work:

$colourId = $product->getResource()->getAttribute("colour")->getSource()->getOptionId("red");
$product->setColour($colourId)->save();
Related Topic