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?

asked Nov 13, 2015 at 15:03

2 Answers 2

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
);
answered Nov 17, 2015 at 13:51
3
  • 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. Commented Nov 17, 2015 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()). Commented Nov 18, 2015 at 12:42
  • @DougMcLean. that's true. Commented Nov 18, 2015 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');
answered Nov 17, 2015 at 13:35
1
  • 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 :) Commented Nov 18, 2015 at 12:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.