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?
2 Answers 2
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 useloadinstead - 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– Doug McLean2015年11月17日 14:22:53 +00:00Commented 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 ofload()).Doug McLean– Doug McLean2015年11月18日 12:42:50 +00:00Commented Nov 18, 2015 at 12:42 -
@DougMcLean. that's true.Marius– Marius2015年11月18日 12:56:13 +00:00Commented Nov 18, 2015 at 12:56
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 usesetId()instead, it works just fine for this purpose. Which of course makes a lovely big difference to speed & resource use :)Doug McLean– Doug McLean2015年11月18日 12:44:20 +00:00Commented Nov 18, 2015 at 12:44
Explore related questions
See similar questions with these tags.