Magento – How to properly save EAV attributes for all store views at once

eavmagento-1.9store-view

We have right now three store views with different locales and plan to extend their quantity in a future.

We encountered issue when some attributes (price, name, etc) are properly received when we saving them with storeId = 0, then, according to

app/code/core/Mage/Eav/Model/Entity/Abstract.php:992

the sql query like this is used to get these attributes:

 SELECT `attr_table`.* FROM `catalog_product_entity_text` AS `attr_table`
 INNER JOIN `eav_entity_attribute` AS `set_table`
 ON attr_table.attribute_id = set_table.attribute_id AND set_table.attribute_set_id = '4' 
 WHERE (attr_table.entity_id = '50') AND (attr_table.store_id IN (0, 2))

As you can see, these is actually works for admin store if attr_table.store_id = 0. But in some cases, like with setting default image for product (image, small_image and thumbnail attributes) or using tag/tag model it is impossible to use storeid = 0.

So, the question is – how in this cases it is possible to update product attributes for all store ids at once without iterating over all store ids like this :

foreach ($stores as $store) {
        Mage::app()->getStore()->setId($store);
        $product = Mage::getModel('catalog/product')->load($product->getId());
        $product->setStoreId($store);
        $product->hasDataChanges(true);
        Mage::getSingleton('catalog/product_action')
               ->updateAttributes(array($product->getEntityId()), 
                                        $attributes, $store); 
    }   

and how if I have for example product with some $id:

    $product = Mage::getModel('catalog/product')->load($id);
    $product->setName('newName')->save(); 
    // how to save this model for all store views  at once, without using setStoreId(0) ?

Thanks !

Best Answer

I'm not sure if I understand you correctly, but I'll try with explaining how the loading of the EAV Attributes actually (should) work(s).

If you save a Product in the Admin without choosing a Store View, the Attributes should all get stored with the default store id = 0.

An attribute in Magento has a Scope, which defines if it can be overridden on website or storeview level.

If you load a Product in the frontend, the values for the attributes are loaded from the current store view / website with a fallback to the default store.

So, if you want to save an attribute with the same value for all store views, you only have to save it with the store id = 0, or just do this in the admin context without setting a store id.

You do not have to save the attribute for every store view (if it does not change between them).

If you have to, you are probably not in the admin context or you have a module installed which messes sth up