2

If you load a product in Store View scope, all the inherited data ("Use Default Values") comes with it... which makes perfect sense, so far so good.

$_product = Mage::getModel('catalog/product')->setStoreId($storeId)->load($productId);

My problem is, if you then change one attribute and save, everything is saved at Store View scope.

$_product->setName($storeSpecificName)->save();

Now, if I look at the product in the admin, every "Use Default Values" checkbox is unticked :(

It doesn't help if I do $_product->save() or $_product->getResource()->save($_product). I even tried:

$_resource = $_product->getResource();
$_resource->isPartialSave(true);
$_resource->save($_product);

Maybe I'm misunderstanding what isPartialSave() does.

So, how do I change one (or more, but not all) attributes at Store View scope?

12

Load is pretty expensive memory wise.
You can use this for a faster update:

Mage::getSingleton('catalog/product_action')->updateAttributes(
    array($productId),
    array('name' => $storeSpecificName),
    $storeId
);
  • That looks like a brilliant solution, thanks Marius! I did actually have the $product already - my code actually runs as an observer so it's using $_product = $event->getProduct(); and I (perhaps wrongly) simplified my question to use load instead - but this way still wins, as I don't have to muck about with changing the scope of the object and potentially interfering with other observers. I'll try it out now. – Doug McLean Nov 17 '15 at 14:22
  • It's probably worth mentioning at this point that this can't be used to unset attributes at store view scope, i.e. to re-tick the "use default values" checkbox - as described in your answer here @Marius. For that, my answer below is working for me as a fallback (especially when I found I could use setId() instead of load()). – Doug McLean Nov 18 '15 at 12:42
  • @DougMcLean. that's true. – Marius Nov 18 '15 at 12:56
6

Found it! Thanks to https://magento.stackexchange.com/a/90342/4090

$_product = Mage::getModel('catalog/product')->setStoreId($storeId)->load($productId);
$_product->setName($storeSpecificName);
$_product->getResource()->saveAttribute($_product, 'name');
  • I found you don't need to load() the product in question... if you use setId() instead, it works just fine for this purpose. Which of course makes a lovely big difference to speed & resource use :) – Doug McLean Nov 18 '15 at 12:44

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.