cancel
Showing results for 
Search instead for 
Did you mean: 

How to set image type (role) into product image programmatically ?

How to set image type (role) into product image programmatically ?

Magento 2.2.x

I want to set product image role programmatically not using product csv import 

so please let us know how can i do that using script?

 

Thanks in advance !!!

3 REPLIES 3

Re: How to set image type (role) into product image programmatically ?

You can load the products and then you can set like this, this is example to set small image

 

foreach ($products as $product){
    $smallImage = $product->getSmallImage();
    $imagePath  = '/catalog/product' . $product->getImage();

    if( $smallImage == 'no_selection' ){
        $product->addImageToMediaGallery( $imagePath, array( 'small_image' ), false, false );
        $product->save();
    }
}

Re: How to set image type (role) into product image programmatically ?

Thanks for reply,

 

I had tried all solutions available in stack overflow or other magento community regarding this but it's not working.

Using your solution, if product image already exists than add another image working well but it it's role was not set.

I need to set product image role into existing product image not want to add new image.

Re: How to set image type (role) into product image programmatically ?

Hello,

 

Please if anyone know solution for this then please give me suggestion