Reset "Use Default Value" for specific store view on all products

Assuming the store id for the french store is 2, you should run the below mysql queries:

DELETE FROM `catalog_product_entity_text` where store_id = 2;
DELETE FROM `catalog_product_entity_datetime` where store_id = 2;
DELETE FROM `catalog_product_entity_decimal` where store_id = 2;
DELETE FROM `catalog_product_entity_int` where store_id = 2;
DELETE FROM `catalog_product_entity_varchar` where store_id = 2;

This basically deletes the attribute values for all attributes and products for which the store id is set to 2. When Magento cannot find the attribute value for a product against the particular store id, it picks the default value.


Unfortunately, none of the efficient ways to update a product attribute work in this case. $product->getResource()->saveAttribute() updates the attribute for all store views even if you set the store ID on the $product object. Mage::getSingleton('catalog/product_action')->updateAttributes() only updates the value in a specific store, but it cannot set the attribute to use the default value (see also this Stack Overflow question for a reference). Hence, we have to use the slow, memory intensive way via $product->save().

I assume that you know which attributes you would like to update. For the following example, I set the visibility attribute to use the default value. The following script should then do the trick (make sure you change it according to your needs):

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

require_once 'app/Mage.php';
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
set_time_limit(0);

function useDefaultValueCallback($args)
{
    // change to the ID of your french store view
    $specificStoreViewId = 7;
    $product = Mage::getModel('catalog/product');
    $product->setData($args['row']);
    $product->setStoreId($specificStoreViewId);
    // change according to your needs
    $product->setData('visibility', false);
    $product->save();
}

$products = Mage::getModel('catalog/product')->getCollection();
Mage::getSingleton('core/resource_iterator')->walk($products->getSelect(), array('useDefaultValueCallback'));

Joining a bit late, but I didn't really like either of the above answers.

  1. Simons answer of walking over the product collection is insanely slow and inefficient, however it is at least using Magento
  2. ParasSood's answer of doing direct modifications on the database is a little scary, and not very usable if you want this wrapped as a bit of automated functionality.

Here's my attempt, not fully tested but it seems to do what I need it to do.

/**
 * If given store code will reset only that store, otherwise will set all stores to "use default"
 *
 * If given product ids will reset only those products, otherwise all products will be set to "use default"
 *
 * @param $attributeCode
 * @param null $storeCode
 * @param null $productIds
 *
 */
public 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
    );
}

Tags:

Store View