开发者

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

开发者 https://www.devze.com 2023-03-24 09:18 出处:网络
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 me

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.


Your 'simple solution' can be even simpler;

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消