Setting Data Per-Store On Entites

Posted Almost 12 years ago. Visible to the public.

Certain entities (Products and Categories, for example) have the concept of a
store scope, whereby you can set the default value for all stores, or you can
set a value on a per-store basis.

You could do it this way, and this is probably how most guides will tell you to
do it, but I'll show you in a minute why not:

$product = Mage::getModel('catalog/product');
$product->setStoreId(1);
$product->load(100);
$product->setName('Store 1 Product Name');
$product->save();

So, you'll probably have noticed that worked, however what you may have also
noticed is that all the products attributes now exist in the store ID 1 scope,
which is a bit arse.

The way to do this without triggering such behaviour is like so:

$product = Mage::getModel('catalog/product');
$product->setStoreId(1);
$product->load(100);
$product->setName('Store 1 Product Name');
$product->getResource()->saveAttribute($product, 'name');

I have seen instances where the above doesn't work, specifically if the attribute
you are trying to unset uses the eav/entity_attribute_backend_serialized
backend model.

To remove an attribute from a scope you just set it to null, HOWEVER, you can't use saveAttribute() to save it (see the resource model for why) - you have to save the whole product, which is really crappy, so you're going to have to make sure you remove all the attributes that shouldn't be defined at this level - how annoying!

$product = Mage::getModel('catalog/product');
$product->setStoreId(1);
$product->load(100);
$product->setName(null);
$product->save();

Note that I've seen an answer on SO Show archive.org snapshot which claims to have a solution - this doesn't work - it has the same failing I observed at the start of the card.

Mike Whitby
Last edit
Over 9 years ago
Mike Whitby
Posted by Mike Whitby to Magento (2012-05-04 10:00)