Magento – Set an attribute value in multishop system

magento-1.7

I'm making a cronjob which sets values for a custom global attribute with attribute code 'color_family'. The value that is given is an array (for example $color_families = array(211, 215, 216, 228)), because the type of 'color_family' is "multiple select".

My code for setting the value:

$_product->setData('color_family', $color_families);
$_product->save();

My problem is, that this works only for one store system. When I run the cronjob on multishop system, the value for this attribute is not updated.

I found this online and tried it:

foreach($_product->getStoreIds() as $storeId) {
    $_product->setStoreId($storeId)
         ->setData('color_family', $color_families);
    $_product->save();
}

but that also brought no result.

I'm working on Magento CE v. 1.7.0.2..

I would really appreciate any advice you could give me.

Best Answer

You can update attribute values in an other way:

Mage::getSingleton('catalog/product_action')->updateAttributes($productIds, $productData, $storeId);

Let's take an example:

Mage::getSingleton('catalog/product_action')->updateAttributes(array(12, 33), array('color_family'=>'211,215,216,228'), 0);

This means that the products with ids 12 and 33 will have the attribute 'color_family' changed to the value '211,215,216,228' for the store id 0 (admin). Since your attribute is global, this should do. You can change one or more products at a time.