Programmatically setting the default image gallery of multimedia files for a store (Magento)

I have a script that adds images to my products. It is used to set images, small_image and thumbnails. The code works well for the default view, but when I switch to save, the multimedia gallery is set to "no_image". Causing my product to not have an image in the interface at all.

I tried to reset repository 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 change certain store attributes and reset them to use the parent?

Thanks.

+8
php magento
source share
2 answers

Your β€œsimple solution” could be even simpler:

 foreach($product->getStoreIds() as $storeId) { $product->setStoreId($storeId) ->setImage(false) ->setSmallImage(false) ->setThumbnail(false) ->save(); } 
+4
source share

ok for those who still have problems, i 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 the default image form in all my store views (ids is 1,2,3,4)

edit: modified SQL as electric Jesus suggested

+2
source share

All Articles