1

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.

4

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.

  • Marius, thank you for your reply. The problem was that I was getting the wrong attribute ids for 'color_family'. There was nothing wrong with my code. And because the ids were not found, could also not be set. – knospe Jul 17 '13 at 8:39
  • :). Anyway. You can use the code above. It's faster than saving a product through the Magento internal API. – Marius Jul 17 '13 at 9:16
  • Thanks for clearing up that bit about store id=0 for global attributes. – Christoffer Bubach Mar 13 '14 at 16:58

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.