I want to parse my product data and ensure all child products in each store view have swatch_image, image, small_image, thumbnail image roles set to the current image.

I have tried

$_store = $this->_storeManagerInterface->getStore($_storeid);


But it doesn't work.

I have also tried

$_product->addImageToMediaGallery($_image, array('swatch_image','image', 'small_image', 'thumbnail'), false, false);

But this seems to replace the main product image and breaks the roles assigned in other store views.

If I look at

        $_existingMediaGalleryEntries = $product->getMediaGalleryEntries();
    foreach ($_existingMediaGalleryEntries as $entry) {

I can see that for store 0 the roles are saved

[imageroles] => Array
                        [0] => Array
                                [0] => image
                                [1] => small_image
                                [2] => thumbnail
                                [3] => swatch_image


but for other store views they are empty. I tried to setype in $_defaultImage=$_product->getMediaGalleryImages()->getFirstItem();

$_defaultImage->setTypes(['swatch','thumbnail', 'small_image', 'image']);

But this has not effect.

What is the correct way to programatically set the store view image role, or ensure that the image and roles are set in all store views resetting any store view settings?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.