magento2: Product::addImageToMediaGallery throws Exception

Hi folks, I’m trying to add some images to a product, but it’s not working like expected.

Preconditions

Magento Version 2.1.1 PHP Version 7.0

Steps to reproduce

$product = $this->productFactory->create()
            ->setName($productName)
            ->setStatus($productStatus)
            ->setSku($productSku)
$product->setAttributeSetId($product->getDefaultAttributeSetId());

$product->addImageToMediaGallery($file, [
                'image',
                'small_image',
                'thumbnail',
            ], false, false);
$this->productRepository->save($product);

Expected result

Image gets added

Actual result

Exception gets thrown:

Notice: Undefined index: media_type in /var/www/magento.fissler.local/vendor/magento/module-catalog/Model/Product.php on line 2527

There’s a workaround for this issue, using the Product::save method instead of the ProductRepositoryInterface::save method, but because it’s a deprecated method I would like to avoid this.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 11
  • Comments: 53 (12 by maintainers)

Commits related to this issue

Most upvoted comments

@veloraven, this issue is present in Magento CE 2.1.6.

Do you require any more replication guidance to get this labeled as “acknowledged” and moved to an internal ticket?

@eduard-kistner You can ignore my answers but i will warn others developers to do not use your module. Do you know why? Because you overwrite magento core files. It is not acceptable to do it, and you know why @eduard-kistner ? Because if i have modules that create plugins (Do you know what it is?) after, before and around method save from ProductRepository they will stop works. I dont even want to think what if someone have modules from other vendors that do something on ProductRepository save plugins. They will have to overwrite all modules to work with your poor module. And for clarity, You create overwrite “module”, You create workaround. I just gave good practice solution without overwriting anything.

And your module? With manual creating directories? Haha! It is good - how you said “trolling”. Did you ever use continous integration? Did you ever have to create project from scrath multiple times? I dont want to even think about your solution if i will have to involve 20 developers to project and told them how to “install” your module. How many this kind of modules do you have in your project? 10, 20? With manually creating directories? Copy files from repository? Missing only: “Next step: copy files by ftp to your production environment.”

@R4c00n, thank you for your report. We’ve created internal ticket(s) MAGETWO-81589 to track progress on the issue.