Magento - set product attribute to use default values

This has been asked many times, but without a working answer.

I have several stores and some attributes have been redefined. I want to change these attributes to "use default" with a script.

Here is an image showing the repository views and the “Use default value” checkboxes http://dl.dropbox.com/u/3209649/storeviews-and-defaultvalues.png (images are not yet allowed)

In app / code / core / Mage / Adminhtml / controller / Catalog / ProductController.php setData () is used with false for the second argument when "Use default" Value "was selected for any attributes.

/** * Check "Use Default Value" checkboxes values */ if ($useDefaults = $this->getRequest()->getPost('use_default')) { foreach ($useDefaults as $attributeCode) { $product->setData($attributeCode, false); } } 

The following code attempts to set the 'name' attribute to use the default values ​​for product 1 in store 3 using the same method.

 require_once '../app/Mage.php'; Mage::app(3); $product = Mage::getModel('catalog/product')->load(1); $product->setData('name', false); # as used in ProductController.php $product->save(); 

Using

 $product->setData('name', 'anything'); 

correctly sets the 'name' attribute to 'nothing' but false does not set it to use the default value

"Use default value" is not stored anywhere in the database, so should there be another procedure inside the controller for the admin interface that deletes the attribute string?

Related links here → http://pastebin.com/raw.php?i=j7fwu9H6 (links are not yet allowed)

+6
source share
1 answer

This does not work, because for this type of work the current store is the admin repository.

To create a view of a specific store, use the default value for this attribute:

 Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); $product = Mage::getModel('catalog/product') ->load($product_id) // in your case: 1 ->setStoreId($store_id) // in your case: 3 ->setData($attr, false) // in your case: 'name' ->save(); 
+12
source

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


All Articles