Magento - save the product without installing "Use the default value"

I have a setup with several stores - StoreA, StoreB and StoreC. And in the controller (using StoreA URL) I edit the product as follows:

$_product = new Mage_Catalog_Model_Product(); $_product->load($productId); $_product->setData('attribute1','somevalue'); $_product->save(); 

If I then go to Admin / Edit Product for this particular product, I found that attribute1 was set correctly, but I also found that if I select StoreA in "Choose Shop View", I found that everything is "Use Default" The values ​​"(for StoreA) are set to false.

Questions:

  • How to change the code above to β€œUse the default value”, the checkboxes for StoreA remain correct.
  • After the above code has been executed (and "Use default" Value "for StoreA is set to false"), how do I return the value "Use default" Value "returns true for StoreA?

Edit:

Screenshot added: enter image description here

+4
magento
source share
1 answer


I don’t quite understand the "Use default" checkboxes that you see, but there are two things that I noticed.

Using the Factory Template

Your code:

 $_product = new Mage_Catalog_Model_Product(); 

Use the factory template that is standard for Magento:

 $_product = Mage::getModel('catalog/product'); 

This in itself is not a problem, but it is something to keep in mind.

Updating Product Attribute Only

Further, if you save only a specific attribute, it will be faster (and potentially avoid your problem) if you update only this attribute. For instance:

 $attribute = array('attribute_code' => 'attribute_value'); Mage::getSingleton('catalog/product_action') ->updateAttributes($_product->getId(), $attribute, 0); 

Link for the updateAttributes () method. My reasoning is that possible defaults are added, preserving the entire product, not just a specific attribute.

If this does not help, perhaps a screenshot of what you see may help me visualize the problem.

+1
source share

Source: https://habr.com/ru/post/908562/


All Articles