Magento 1 – How to Set Selected Value in Multiselect Attribute

event-observermultiselect-attributeproduct

How to set selected value in multiselect attribute progmatically. I tried this one but it doesn't work for me.

How to Programmatically set a Product's Multi-Select Attribute by Labels

the code on the link above doesn't stop from loading

$productObj = Mage::getModel('catalog/product')->load($product->getId());
$productObj->setData('filter_category','51,52');
$productObj->save();    

the code above is invoke after catalog_product_save_after

Best Answer

Since you want to do this in the action catalog_product_save_after you could simple update the individual product attribute and not the complete product, thus stopping the infinite loop problem.

$attrCode = 'your_attribute';
$sourceModel = Mage::getModel('catalog/product')->getResource()
    ->getAttribute($attrCode)->getSource();
$valuesText = explode(',', 'red,green,blue');
$valuesIds = array_map(array($sourceModel, 'getOptionId'), $valuesText);
$product->setData($attrCode, $valuesIds);
$product->getResource()->saveAttribute($product, $attrCode);