How to set use default for product attributes in bulk updates in Magento

Today I have faced some issues while working on updating the attributes in Magento. By mistakenly I set ‘New From Date’ and ‘New To Date’ to incorrect value in store view level.  But I set the correct value In default config. So for each product I need to click the checkbox to ‘use default’ value. I did the same for all the products. So I need to check the use default for all the products in the specific store.

To check the use default for all the products we need to run the below program.


<?php

ini_set('memory_limit', '-1');

//error_reporting(E_ALL | E_STRICT);

$mageFilename = '../app/Mage.php';

require_once $mageFilename;

Mage::setIsDeveloperMode(true);

umask(0);

Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));

 function forceProductsToUseDefault($attributeCode, $storeCode = null, $productIds = null)

    {

        $conditions = array();

    if (is_null($storeCode)) {

        $conditions['store_id != ?'] = Mage_Core_Model_App::ADMIN_STORE_ID;

    } else {

        $store = Mage::app()->getStore($storeCode);

        if (!$store instanceof Mage_Core_Model_Store || !$store->getId()) {

            Mage::throwException("Store with code not found: $storeCode");

        }

        $conditions['store_id = ?' ] = $store->getId();

    }

    if (!is_null($productIds)) {

        $conditions['entity_id in(?)'] = $productIds;

    }

    $attribute = Mage::getModel('eav/entity_attribute')

        ->loadByCode(Mage_Catalog_Model_Product::ENTITY, $attributeCode);

    if (!$attribute instanceof Mage_Eav_Model_Entity_Attribute_Abstract || !$attribute->getId()) {

        Mage::throwException("Attribute with code not found: $attributeCode");

    }

    $conditions['attribute_id = ?'] = $attribute->getId();

    $coreResource = Mage::getSingleton('core/resource');

    $coreResource->getConnection('core_write')->delete(

        $coreResource->getTableName(array('catalog/product', $attribute->getData('backend_type'))),

        $conditions

    );

    }

                forceProductsToUseDefault('news_from_date', 1);

    forceProductsToUseDefault('news_to_date', 1);

?>

Leave a Comment

Your email address will not be published. Required fields are marked *


8 + = 13