Set Attributes to ‘Use Default Value’ for a List of Products

attributesproducts

I want to set the images to 'use default value' for a list of product, and for a list of store view. I know how to do it individually for each product : setData(attributeName,false), and so i can do a loop over my list of product. Problem: it's really too slow.

$attrArray=array('thumbnail','small_image','image');
$products = array(170,171,172);
$stores = array(17,18,19);
foreach ($stores as $store_id) {
    foreach ($products as $product_id) {
        foreach ($attrArray as $attr) { 
            $product = Mage::getModel('catalog/product')
            ->load($product_id)->setStoreId($store_id)
            ->setData($attr, false)
            ->save();
        }
    }
}

So i've try to use Mage::getSingleton('catalog/product_action')->updateAttributes($products, $attrArray, $store_id); instead, which is supposed to do the same thing but over a list of products.
It actually do something : all my images are now set to 'no images', but not to 'Use default value' as expected.

$attrArray = array('thumbnail'=>false,'small_image'=>false,'image'=>false);
$products = array(170,171,172);
$stores = array(17,18,19);
foreach ($stores as $store_id) {
    Mage::getSingleton('catalog/product_action')
    ->updateAttributes($products, $attrArray, $store_id);
}

If someone around here have an idea, it could really help me to save some time ! Thanks.

Best Answer

Basically, setting an attribute value to 'Use default values' means that you have to delete the row in the database for that attribute, for the specific product, for a store id.
Here is a simple solution that does that. It requires altering the database directly and some people will say that this is a big 'No-No' but it works.

$attrArray=array('thumbnail','small_image','image');
$products = array(170,171,172);
$stores = array(17,18,19);
$productsAsString = implode(',', $products);
$storesAsString = implode(',', $stores);
//get access to the resource
$resource = Mage::getSingleton('core/resource');
//get access to the db write connection
$connection = $resource->getConnection('core_write');
//model for retrieving attributes
$eavConfig = Mage::getModel('eav/config');
$tables = array();
//get the association between attribute ids and the tables where their values are stored
//group them by table name so you will run a single query for each table
foreach ($attrArray as $attributeCode){
    $attribute = $eavConfig->getAttribute('catalog_product', $attributeCode);
    if ($attribute){
        $tableName = $resource->getTableName('catalog/product') . '_' . $attribute->getBackendType();
        $tables[$tableName][] = $attribute->getId();
    }
}
//for each table delete the attribute values in the specified store for the specified products
foreach ($tables as $tableName => $attributeIds){
    $attributeIdsAsString = implode(',', $attributeIds);
    $q = "DELETE FROM {$tableName}
                WHERE
                    attribute_id IN ({$attributeIdsAsString}) AND
                    entity_id IN ({$productsAsString}) AND
                    store_id IN ({$storesAsString})";
    $connection->query($q);
}

This should be it. But in case I'm over-confident and this doesn't work, back-up your database first.