Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically setting default media gallery image for Store View (Magento)

Tags:

php

magento

I have a script that adds images to my products. It is used to set the image, small_image and thumbnail. The code works nice for the default view, but when I switch to store view the media gallery is set to "no_image". Causing my product to have no image at all in the frontend.

I tried to reset the store view attributes without success.

$product->addImageToMediaGallery($fileName, array('image', 'small_image', 'thumbnail'), false, false);
$attributes = $product->setStoreId(1)->getTypeInstance(true)->getSetAttributes($product);
if (isset($attributes['media_gallery'])) {
    $attributes['media_gallery']->getBackend()->clearMediaAttribute($product, array('image', 'small_image', 'thumbnail'));
}
$product->save(); 

How can I modify the specific store attributes, and reset them to use the parent one?

Thank you.

like image 436
Fred Avatar asked Aug 01 '11 02:08

Fred


2 Answers

Your 'simple solution' can be even simpler;

foreach($product->getStoreIds() as $storeId) {
    $product->setStoreId($storeId)
        ->setImage(false)
        ->setSmallImage(false)
        ->setThumbnail(false)
        ->save();
}
like image 98
clockworkgeek Avatar answered Nov 02 '22 14:11

clockworkgeek


ok for those who still have problems i have found sql solution:

DELETE FROM `catalog_product_entity_varchar` 
WHERE store_id IN (1,2,3,4)
      and entity_type_id=4 
      and (attribute_id=77 or attribute_id=78 or attribute_id=79);

in my case i wanted to restore default picture view form all my store views (ids are 1,2,3,4)

edit: modified SQL as Electric Jesus suggested

like image 39
karantan Avatar answered Nov 02 '22 15:11

karantan