Updating attributes in Magento 2

Today we have faced some issues while working on updating the attributes in Magento.

Magento2: Use default for product attributes in bulk updates
If we had  set ‘New From Date’ and ‘New To Date’ to an incorrect value in store view level, But later to set the correct value In default config is a hectic to set them  So for each product by clicking the checkbox to ‘use default’ value. Have done the same for all the products by using the below script which updates them altogether.

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 *