0

I have that in my backoffice : witout

and I need that: with

This solution does not work for me :

  if($key == 1){
        $prod->addImageToMediaGallery($imgPath, array('image', 'small_image','thumbnail'), false, false);
    }else{
        $prod->addImageToMediaGallery($imgPath, null, false, false);
    }
    $prod->save();

How can I do ?

Thank you

EDIT : my new implementation, but it is not working :

if(!$productFactory) $productFactory=$objectManager->create('Magento\Catalog\Model\ProductFactory');
$product=$productFactory->create()->load($id_product_for_image);
$product->addImageToMediaGallery($tmpName, $is_cover ? array('image', 'thumbnail', 'small_image') : array(), false, false);

I think it is connected to this problem : https://github.com/magento/magento2/issues/34658

1 Answer 1

0

Use the Following code to overcome this

/** \Magento\Catalog\Model\ProductFactory $product */
$product = $this->productFactory->create();
foreach ($totalImages as $index => $path) {
   $type = [];
   if ($index == 0) {
       $type = ['image', 'thumbnail', 'small_image'];
   }
   $product->addImageToMediaGallery(
             $path,
             $type,
             false,
             false
   );
}
2
  • sorry, I know this is a relevant and obvious way to do it but it doesn't work for me. The images remain as if without a role in both cases. It may depend on the magento version.
    – idIA Tech
    Apr 13, 2022 at 17:05
  • my magento version is 2.4.3-p1. You can see my personnal implementation on the first post. But I have tested it on Magento ver. 2.3.7-p2 and it is working !!
    – idIA Tech
    Apr 13, 2022 at 17:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.