0

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);
$this->_storeManagerInterface->setCurrentStore($_store);

$_product->setThumbnail($_image);
$this->_productRepository->save($_product);

But it doesn't work.

I have also tried

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

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();
    $_imageRoles=array();
    foreach ($_existingMediaGalleryEntries as $entry) {
        $_imageRoles[]=$entry->getTypes();
    }

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=$_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.