10

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.

8

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.

|improve this answer|||||
  • 1
    Thanks a lot, i didn't test it yet, as i don't need it anymore and don't have a test server for the moment, but it will be useful later for sure ! – Esteban Oct 2 '13 at 11:45
  • I'll vouch for the code. Works well! – mpw Feb 7 '14 at 17:49
  • it works well and fast! – electroid Nov 25 '16 at 12:35
  • I want to set "Use default value" checked for all the product attributes in Magento 2, I am having issue with product attributes value, they are being shown from default store view but few attributes are not set to "Use default value" as checked. So whenever I update these product attributes value for all store view that doesn't reflect at frontend. – Himmat Paliwal Aug 27 '17 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.