Magento – Use all images from the default store on the new store view

ee-1.13mage-shellPHPproductproduct-images

I have a new store view and i've updated all the products using the

magento admin: Manage Products > Select all > Update Attributes

and then selecting the new store. Now that all my products are affiliated with my new store view i'm having a problem with all my images. On the front end it shows all placeholders. In the admin panel when i click on a product and then images.. it may have images but it's not selected to use base image, small image or thumbnail.

Is there a way to update all images on my new store view to use the default store view main images?

require_once 'abstract.php';

class Attach_Default_Store_Images Extends Mage_Shell_Abstract {

    public function run()
    {
        $products = Mage::getModel('catalog/product')->getCollection();
        foreach ($products as $product) {
            $productFrom = $product->setStoreId(1)->getImage();
            $productTo = $product->setStoreId(13)
            ->setImage($productFrom)
            ->setSmallImage($productFrom)
            ->setThumbnail($productFrom);
            echo "Images Updated\n";
            $product->save();
   }

        Mage::getModel('catalog/product_image')->clearCache();
        echo "Image Cache Cleared\n";

    }


    public function usageHelp()
    {
        return <<<USAGE
Usage:  php -f cache.php -- [options]
        php -f cache.php -- clean

  clean             Clean Old Cache
  help              This help

USAGE;
    }
}

$shell = new Attach_Default_Store_Images();
$shell->run();

Run a shell script with above?

Best Answer

One solution could be to delete the entries directly in the Database.
In the table catalog_product_entity_varchar you can find all images with the associated store_id. If there is no entry with a specific store_id, Magento will use the value with the store_id 0 (Default StoreView)

With this Query you see all Images which are set in a StoreView

SELECT
    attributes.entity_id, attributes.store_id, attributes.value,  eav.attribute_code
FROM
    catalog_product_entity_varchar attributes
    JOIN eav_attribute eav ON attributes.attribute_id = eav.attribute_id
WHERE 
    eav.attribute_code IN ("image", "small_image", "thumbnail") 
    AND attributes.store_id > 0

What the Query does is, it selects all default image attributes image, small_image and thumbnail with a store_id bigger then 0.
I used the eav_attribute table to select all entries with the attribute codes, because sometime the IDs are different.

The Result looks like this (in your case its a bigger list):

entity_id   store_id   value            attribute_code
86          6          /i/a/iamge1.png  image
86          6          /i/a/iamge2.png  small_image
86          6          /i/a/iamge2.png  thumbnail

Now we can test if this works like expected. If we choose a Product ID (entity_id) and change the images in the adminpanel to Use Default Value and execute the Query again, the entries are removed from the table

So, we can convert this select Query into a delete Query

DELETE FROM 
    attributes.entity_id, attributes.store_id, attributes.value,  eav.attribute_code
FROM
    catalog_product_entity_varchar attributes
    JOIN eav_attribute eav ON attributes.attribute_id = eav.attribute_id
WHERE 
    eav.attribute_code IN ("image", "small_image", "thumbnail") 
    AND attributes.store_id > 0

Before executing the delete query on your production system, you should test it on a test system.

In the case you want to change it for a specific StoreView, just change the WHERE clause to

AND attributes.store_id = 12 //Your StoreView ID
Related Topic