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

Viewed 9763

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.

2 Answers
Related