-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Product::addImageToMediaGallery throws Exception #6803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I've encountered an additional bug, when using the
The |
The same problem for me - strange is that is was working previously but it stopped after base url for media files was changed |
The same problem here, any chance to fix it? |
Same here, 3 months gone and no response ?! |
hello @sparrowek, @szpadyzor, @eduard-kistner, could you please clarify which versions do you use? |
@KrystynaKabannyk version 2.1.1, PHP 7.0, like in 1st post. |
shure:
In my humble opinion the only missing part is that in The workaround with deprecated save() ( mainly ) works for me. |
I did some more research as the change mentioned above does not work completely well. It will seems to fit as no more exception will be thrown, but no images will be shown on product side then. The main Problem seems then to be that the method processMediaGallery() will not recognize entries which correspond to an file. In the end i have created an module to fix that. |
Also ran into this problem today. If this is not in it's correct place here, I'm sorry |
I am unable to get this working either, having tried a variety of approaches. Here's what I have tried:
Nothing has worked. I'm on Magento 2.1.5, PHP 7.0.15 The only thing I seem to be doing differently than others is setting the What happens for me is an image is associated with the product, but none of 'roles' are set in the admin when I view the product. If I set the roles manually in the admin, after a programmatic load, it seems to work. Strangely the admin doesn't seem to be using That said, I haven't taken the time to break out the debugger to see what kind of workaround might actually work, though I'm sure there is one lurking behind a debug session. |
@quickshiftin But maybe we should not spam this ticket ;) |
@eduard-kistner I went ahead an made a module to demonstrate the problem. Maybe there's a glaring error in there you could point out :) |
@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? |
Any update on this? |
I am sorry all of You to hear that, but You all do it wrong. First You have to create media entry:
Then You have to set it on product:
and then You have to add image to media gallery:
|
Seriously, are you just trolling? Sp maybe you can clarify what methods exactly you are using? |
@eduard-kistner sry Man, my bad: should be \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory instead of \Magento\Catalog\Api\ProductAttributeMediaGalleryManagementInterface. I am correcting it. |
Thanks for the clarification but your code does also throw the mentioned exception ( tested with Magento 2.1.7 ). I will try to give an step by step example what i mean:
'media_gallery' => [
'images' => [
0 => [
'value_id' = null,
'file' = null,
'label' = null,
'position' = null,
'disabled' = null,
'types' = null,
'media_type' = 'image',
'content' = null,
],
1 => [
'file' = 'some_path',
'label' = '',
'position' = 1,
'disabled' = 0,
]
]
]
So in my opinion its surly generally better to use the Gallery/Entry abstractions. Also as this method will be the one mostly used IMO its not an solution to shout that everyone is doing it false but to fix it the way everyone uses it ( or remove that ). All in all @magento-team should refactor this as mentioned before ;) |
anyone were able to add an image to product programmatically? |
@Dukeofvampires, Yes: 1Install this: https://github.com/DIE-KAVALLERIE/magento2-product-image-fix 2/**
* @var \Magento\Catalog\Model\ProductRepository
*/
protected $productRepository;
/**
* @var MagentoProduct\Gallery\Processor
*/
protected $galleryProcessor;
...
// Delete existing Magento images from DB and filesystem
$existingMedia = $product->getMediaGalleryEntries();
if(is_array($existingMedia) && count($existingMedia) > 0) {
foreach($existingMedia as $existingMediaItem) {
$this->galleryProcessor->removeImage($product, $existingMediaItem['file']);
}
// @todo: delete images without saving here. Each Magento save is computationally expensive.
$this->productRepository->save($product);
}
// Import new images
$product->setMediaGalleryEntries([]);
$newImages = [...];
foreach($newImages as $i => $newImage) {
$imageRoles = $i == 0 ? ['image', 'small_image', 'thumbnail'] : [];
$product->addImageToMediaGallery($newImage['local_path'], $imageRoles, true, false);
}
$this->productRepository->save($product); |
Thank you, this will do as a temporary solution. Also how magento itself is saving uploaded images through admin? |
@Dukeofvampires, as with most issues on magento2, there's probably a year-old fix in develop branch that hasn't been pushed to release :( I don't know how Magento admin gets round it. Due to time constraints, I gave up debugging and applied @eduard-kistner's patch. |
@JacobDrummond That's a problem with M2 using deprecated methods in it's core. So probably I would prefer using patch as well. |
@Dukeofvampires, not I see this all the time throughout the codebase: Magento devs breaking their own rules. I would guess this core stuff was written before the coding standards were finalised. |
well, yes, thats what I meant. Technically Model->save(); is a shortcut to $this->_getResource()->save($this) ;) I'm pretty sure most of this is present because lots of the code is ported from M1. but it's better to stop flooding the issue. :) |
@quickshiftin I need to know if you find any solution to your problem. I have the same issue, image gets added to product but no image role it's set |
@GraysonAstral still don't know why you want to overwrite core magento mechanism. I gave you working solution. And what does it mean that "code in my project didn't work" ? Some exception is thrown or what? |
Don't know why @szpadyzor comes again with not working code examples ( see my comment here ) but did ya tried https://github.com/DIE-KAVALLERIE/magento2-product-image-fix ? |
I will tell you why, because @eduard-kistner never test my implementation, you wrote: There is said: To simply debug it, just check how it is done in admin panel. Product on adminhtml is saved by Do you know what it does? It creates media gallery entries. In line 478 in Show me your previous implementation, and show me where did you set up You can create new media entry it is better to do it because you can set not only images but videos or other media types. Maybe you saw that Debug method I will tell it again: You dont have to overwrite magento Gallery Processor just understand the code. Of course saving product by Debug it @eduard-kistner and then argue with me and lever my solution. |
@szpadyzor The mentioned module is long time on min. one Live instance and others also use it. From now on i will simply ignore you till you have constructive informations. |
@eduard-kistner 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? |
Hello Folks, I am trying to resolve this issue with flow the way it should work its taking time more than usual. this only for update from my side if any buddy resolved this or found any way to resolve this by stranded way Lets us know here please. |
Hi @progreg. Thank you for working on this issue.
|
Hi @R4c00n. Thank you for your report.
The fix will be available with the upcoming 2.3.1 release. |
Uh oh!
There was an error while loading. Please reload this page.
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
Expected result
Image gets added
Actual result
Exception gets thrown:
There's a workaround for this issue, using the
Product::save
method instead of theProductRepositoryInterface::save
method, but because it's a deprecated method I would like to avoid this.The text was updated successfully, but these errors were encountered: