Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento setting Product Attribute's "Use Default Value" using updateAttributes

Tags:

magento

I have a multi-store setup and I am setting a Product's Attribute for a particular store to use the "Use Default Value" option - (ie to use the value in the Store View), as follows:

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

This sets the Name attribute of storeId for $productId to use "Use Default Value"

Given that I have a lot of attributes to set I am trying to use:

Mage::getSingleton('catalog/product_action')->updateAttributes(array($productId), array('name' => false), $storeId);

But this is not setting the "Use Default Value" checkbox to true.

How can I use ->updateAttributes to set a store value to use the "Use Default Value" option?

Screenshot:

enter image description here

like image 483
mas Avatar asked Feb 15 '12 22:02

mas


2 Answers

The "Use Default Value" flag isn't stored in the database anywhere.

Magento core uses that flag to do this when saving products:

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

Before doing some other things.

I would look at Mage_Adminhtml_Catalog_ProductController (app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php) and learn how Magento core does it.

Specifically saveAction() and _initProductSave()

I hope this points you in the right direction.

like image 50
Magento Guy Avatar answered Nov 15 '22 12:11

Magento Guy


Simply use 0 as the store ID (admin store) which is the same thing as default values in the Magento Admin.

Mage::getSingleton('catalog/product_action')
    ->updateAttributes(
        array($productId),
        array('name' => false),
        0);

If you already set the store view scopes you will have to go in and recheck the use default values or it will override the attribute at the relevant scope.

There may be a way to programmatically set these. I'm uncertain.

like image 42
auroraexlux Avatar answered Nov 15 '22 13:11

auroraexlux