When importing product data the product images are being duplicated for each import run.
<install dir>
<install dir>
test-1.jpg
, test-2.jpg
, test-3.jpg
and test-4.jpg
inside the path <install dir>/var/import-images
. Any valid jpeg will do."sku","product_online","website_id","store_view_code","attribute_set_code","product_type","categories","name","description","short_description","visibility","price","special_price","special_price_from_date","special_price_to_date","url_key","meta_title","meta_keywords","meta_description","display_product_options_in","msrp_display_actual_price_type","additional_attributes","qty","out_of_stock_qty","use_config_min_qty","is_qty_decimal","allow_backorders","use_config_backorders","min_cart_qty","use_config_min_sale_qty","max_cart_qty","use_config_max_sale_qty","is_in_stock","use_config_notify_stock_qty","manage_stock","use_config_manage_stock","use_config_qty_increments","qty_increments","use_config_enable_qty_inc","enable_qty_increments","is_decimal_divided","deferred_stock_update","use_config_deferred_stock_update","related_skus","crosssell_skus","upsell_skus","custom_options","bundle_price_type","bundle_sku_type","bundle_price_view","bundle_weight_type","bundle_values","associated_skus","base_image","base_image_label","small_image","small_image_label","thumbnail_image","thumbnail_image_label","swatch_image","swatch_image_label","additional_images","additional_image_labels","configurable_variations"
"some-test-product",1,1,,"Default","simple","Default Category/Some Category/Some Test Product","Some test product","This is just some test product",,"Catalog, Search",2999,,,,"some-test-product","Some test product",,,"Block after Info Column","Use config",,100,0,1,0,0,1,1,0,0,1,1,1,0,1,1,0,1,0,0,0,1,,,,,,,,,,,"test-1.jpg",,"test-1.jpg",,"test-1.jpg",,"test-1.jpg",,"test-3.jpg",,
"another-test-product",1,1,,"Default","simple","Default Category/Some Category/Some Test Product","Some test product","This is just some test product",,"Catalog, Search",2999,,,,"another-test-product","Another test product",,,"Block after Info Column","Use config",,100,0,1,0,0,1,1,0,0,1,1,1,0,1,1,0,1,0,0,0,1,,,,,,,,,,,"test-2.jpg",,"test-2.jpg",,"test-2.jpg",,"test-2.jpg",,"test-4.jpg",,
__EMPTY__VALUE__
(default)Products end up with each product image only attached once.
Products end up with all product images duplicated.
Hi @simonworkhouse. Thank you for your report.
To help us process this issue please make sure that you provided the following information:
Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:
@magento-engcom-team give me 2.3-develop instance
- upcoming 2.3.x release
For more details, please, review the Magento Contributor Assistant documentation.
@simonworkhouse do you confirm that you was able to reproduce the issue on vanilla Magento instance following steps to reproduce?
Hi @engcom-backlog-nazar. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:
Issue: Format is valid
will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid
appears.[ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description
label to the issue by yourself.
[ ] 3. Add Component: XXXXX
label(s) to the ticket, indicating the components it may be related to.
[ ] 4. Verify that the issue is reproducible on 2.3-develop
branchDetails
- Add the comment @magento-engcom-team give me 2.3-develop instance
to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.3-develop
branch, please, add the label Reproduced on 2.3.x
.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
[ ] 5. Verify that the issue is reproducible on 2.2-develop
branch. Details
- Add the comment @magento-engcom-team give me 2.2-develop instance
to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.2-develop
branch, please add the label Reproduced on 2.2.x
_Next steps are available in case you are a member of Community Maintainers._
[ ] 6. Add label Issue: Confirmed
once verification is complete.
[ ] 7. Make sure that automatic system confirms that report has been added to the backlog.
@magento-engcom-team give me 2.3-develop instance
Hi @dipti2jcommerce. Thank you for your request. I'm working on Magento 2.3-develop instance for you
Hi @dipti2jcommerce, here is your Magento instance.
Admin access: https://i-21885-2-3-develop.instances.magento-community.engineering/admin
Login: admin
Password: 123123q
Instance will be terminated in up to 3 hours.
:white_check_mark: Confirmed by @engcom-backlog-nazar
Thank you for verifying the issue. Based on the provided information internal tickets MAGETWO-98840
were created
Issue Available: @engcom-backlog-nazar, _You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself._
I would like to share some point about the issue. It happens in Magento\CatalogImportExport\Model\Import\Product.php line 1772. If add a variable for exists images in mapper like _media_is_disabled, it should be fix it but the code try to match it with path. That why it doesn't work.
Thank you for the information @tnsezer
Duplicate of #14398
See PR here: https://github.com/magento/magento2/pull/21146
Actually, there's a better PR here, based on mine but it also deals with image deletion: https://github.com/magento/magento2/pull/21855
@erfanimani thanks for the update. Are the issues duplicated or the PRs?
@dmanners Both actually 鈥斅爐his issue is a duplicate of #14398
But then there are two open PRs (I referenced them both in case someone wants to create a patch for it):
I am working on this at #dmcdindia1
@soundararajanm1990 thank you for joining. Please accept team invitation here and self-assign the issue.
Hi @soundararajanm1990. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:
Component: XXXXX
label(s) to the ticket, indicating the components it may be related to.[ ] 2. Verify that the issue is reproducible on 2.3-develop
branchDetails
- Add the comment @magento-engcom-team give me 2.3-develop instance
to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.3-develop
branch, please, add the label Reproduced on 2.3.x
.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
[ ] 3. Verify that the issue is reproducible on 2.2-develop
branch. Details
- Add the comment @magento-engcom-team give me 2.2-develop instance
to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.2-develop
branch, please add the label Reproduced on 2.2.x
[ ] 4. If the issue is not relevant or is not reproducible any more, feel free to close it.
Hi @soundararajanm1990,
Have you fixed this issue? its also coming when trying to Duplicate product.
Thanks
Yogesh
what's the status on this issue?
We can't solve this issue. Still now pending.
We are encountering this too and since the import is ran daily this really clutters up the media gallery. Is there any workaround anyone can recommend?
I have same problem, magento2.3.2 duplicated images after import product csv.
Did you find a solution ?
Two workarounds exist, scroll up to see the relevant pull requests/commits. You'll need a developer to create and apply Composer patches for your Magento version. Seems Magento is too busy to review/merge and fix the bug themselves though..
@erfanimani Yes, we indeed used a combination of the solutions (with some minor edits). Really odd that Magento doesn't look at the fixes because the importer is a vital part of most webshops.
Thank you @erfanimani
With a combination of the solutions, the import works very well.
+1 Hoping to see this merged in soon, big help this :)
Wow same trouble for us... This is awsome magento let this kind of BUG in a 2.3.2 version released worldwild...
@kevinvuillemin can you share the working solution ? I'm strugeling with all the PR closed and open speaking around it. Thank you in advance
@skapin, of course ! how do you want me to share the file?
@kevinvuillemin you can send me a mail to skapinthefourb gmailcom address for example, or create a repository
As you want. Thank you so much :)
@skapin that's good for you ?
https://gist.github.com/kevinvuillemin/802cc5e4b476e73c1c5838b143bd6a51
@kevinvuillemin It works on the first try. Copy/paste instead of Product.php and _voila_ !
Magento version : 2.3.2
Et voila !
Au plaisir ;-)
@kevinvuillemin Your code not working in Magento version : 2.3.1
Working with 2.3.2 for me, are you sure ?
yes we used 2.3.1 not working
@soundararajanm1990 I think they have changed the Product.php class for the release of Magento 2.3.2.
The easier is to upgrade (maintenance & safety)
@kevinvuillemin They moved the issue #14398 onto a new phase - does this mean your PR is ready for Testing on the next Dev version?
any update on this issue?
Below is a more permanent solution.
Be careful, this will trash previous duplicates if there were any.
Make a preference on Magento\CatalogImportExport\Model\Import\Product
and copy/paste this code in it:
```
use const DIRECTORY_SEPARATOR;
class Product extends Magento\CatalogImportExport\Model\Import\Product
{
private $bunch;
private $imagesToRemove;
protected function getExistingImages($bunch)
{
$this->bunch = $bunch;
return parent::getExistingImages($bunch);
}
public function addImageHashes(&$imagesBySku)
{
parent::addImageHashes($imagesBySku);
// get existing images (from db)
$existingImages = [];
foreach ($imagesBySku as $sku => $images) {
foreach ($images as $path => $imageInfo) {
if (!isset($existingImages[$imageInfo['hash']])) {
$existingImages[$imageInfo['hash']] = [];
}
$existingImages[$imageInfo['hash']][] = [
'value_id' => $imageInfo['value_id'],
'path' => $path
];
}
}
// get imported images (from file)
$importDir = $this->_mediaDirectory->getAbsolutePath($this->getImportDir());
$importedImages = [];
foreach ($this->bunch as $rowData) {
foreach ($this->getImagesFromRow($rowData)[0] as $imagesFromRow) {
$imageNames = explode($this->getMultipleValueSeparator(), $imagesFromRow[0]);
$imageHashes = array_flip(array_map(function($imageName) use ($importDir) {
$filename = $importDir . DIRECTORY_SEPARATOR . $imageName;
return $this->_mediaDirectory->isReadable($filename) ? md5_file($filename) : '';
}, $imageNames));
$importedImages = array_merge($importedImages, $imageHashes);
}
}
// guess images to remove
$this->imagesToRemove = array_diff_key($existingImages, $importedImages);
}
protected function _saveMediaGallery(array $mediaGalleryData)
{
// remove duplicate images
$valueIds = [];
if (!empty($this->imagesToRemove)) {
// from disk
foreach ($this->imagesToRemove as $imagesToRemove) {
foreach ($imagesToRemove as $imageToRemove) {
$imagePath = 'pub/media/catalog/product' . $imageToRemove['path'];
if ($this->_mediaDirectory->isExist($imagePath)) {
$this->_mediaDirectory->delete($imagePath);
}
$valueIds[] = $imageToRemove['value_id'];
}
}
// from database
$this->getConnection()->delete(
$this->getConnection()->getTableName('catalog_product_entity_media_gallery'),
$this->getConnection()->quoteInto('value_id IN (?)', $valueIds)
);
}
return parent::_saveMediaGallery($mediaGalleryData);
}
}
Well sadly this looks like it missed the 2.3.3 ship :(
Realy ? That鈥檚 not cool.
But This fix working ? :
https://gist.github.com/kevinvuillemin/802cc5e4b476e73c1c5838b143bd6a51
Normally I would be surprised that it has been so long without a resolution, but I have come to expect this from Magento. Anyway, I have had to resolve this issue for a project recently and it appears that the offending commit was https://github.com/magento/magento2/commit/7803eed11f8bf490bf8905ab0577091c7e782156
I worked around this by adding a preference for a class that extends Magento\CatalogImportExport\Model\Import\Product
and just overrides the uploadMediaFiles(...)
function to force the $renameFileOff
variable to be true
.
This addresses the issue in our specific use case, but it does not address the underlying issues with the way that images are managed in Magento. Additionally, it will also undo whatever "fix" that commit was attempting to do.
@simonworkhouse
Can you please share the code with me?
version 2.3.3
Atttached is the patch that is used to fix the issue in our use case. Magento import is expecting images to have the dynamic paths it created on export. However, the CSV is hand crafted and the image names are plain; "blablabla.jpg" as opposed to "/b/l/a/blablabla_60.jpg". This patch works repeatedly because the CSV file is from the same feed. The patch basically strips off the "/b/l/a/" and "_60" and makes a match to the existing image(s) for that product.
Does anyone have a fix for 2.3.3 with an including fix to remove duplicates? Our server just exploded to 55gb on images because of the duplication during the import..
the commit that @simonworkhouse referred to, was the suicide bomber in our case as well.
Patching that to the previous implementation fixed it.
In my opinion this really should be reverted.
@jalung 's patch worked for me, except for that Magento 2.3.3 (if not earlier versions) renames images all lowercase, so the image name preg_match needs to be case insensitive. Patch with that change here: Product-revised-version.txt
Product-revised-version.txt
Fixed case where CSV row has new and existing images.
Also missed the 2.3.4 ship. @jalung 's patch is working fine for me in 2.3.4. Still needs more testing, for my case, where images are updated by sku, but have same name.jpg.
Possible fix in https://github.com/magento/magento2/pull/26713
I've seen some change in the codebase of Magento 2.3.5 which might fix this issue. Would be great if somebody could verify this after 2.3.5 is officially released on 28 April.
Thank @hostep , let's hope that's the case! I should be able to verify soon after the release.
@erfanimani
OK, I can confirm that the images are now no more duplicated on import.
That is very good...
BUT.....
The Add/Update does not delete images either.
Example:
I import a product (sample_1) with 8 images => fine
Add/Import the product sample_1 with 2 images .... the product is not correctly updated, still 8 images in the db.
@jg-development, if you're importing your images from disk, my fix should work. If you're using urls, don't bother trying my fix.
@Yonn-Trimoreau
I am importing from disk.
Should your fix including the deleting of existing images?
Yes
hmmm did not work for me.
sku,store_view_code,attribute_set_code,product_type,categories,product_websites,name,description,short_description,weight,product_online,tax_class_name,visibility,price,special_price,special_price_from_date,special_price_to_date,url_key,meta_title,meta_keywords,meta_description,base_image,base_image_label,small_image,small_image_label,thumbnail_image,thumbnail_image_label,created_at,updated_at,new_from_date,new_to_date,display_product_options_in,map_price,msrp_price,map_enabled,gift_message_available,custom_design,custom_design_from,custom_design_to,custom_layout_update,page_layout,product_options_container,msrp_display_actual_price_type,country_of_manufacture,additional_attributes,qty,out_of_stock_qty,use_config_min_qty,is_qty_decimal,allow_backorders,use_config_backorders,min_cart_qty,use_config_min_sale_qty,max_cart_qty,use_config_max_sale_qty,is_in_stock,notify_on_stock_below,use_config_notify_stock_qtymanage_stock,use_config_manage_stock,use_config_qty_increments,qty_increments,use_config_enable_qty_inc,enable_qty_increments,is_decimal_divided,website_id,related_skus,related_position,crosssell_skus,crosssell_position,upsell_skus,upsell_position,additional_images,additional_image_labels,custom_options,_super_products_sku,_super_attribute_code,_super_attribute_option,_associated_sku,_associated_default_qty,_associated_position
P06342,,Default,simple,Default Category/Blumen/Rosenknospe,base,P06342,,,,1,,"Catalog, Search",1.05,,,,,,,,P06342-oR-Alt-Copper.jpg,,P06342-oR-Alt-Gold.jpg,,P06342-oR-Alt-Messing.jpg,,,,,,,,,,,,,,,,,,,"usage=Anh盲nger,further_processing=,length=30.1,width=46.4,height=7,rings=6,stone_nr=,stone_size=,material=To,material_form=blech,material_width=52,material_strength=0.6,net_weight=4960,gross_weight=10220,presses=Yes,presses_tool=Yes,presses_machine=360,first_breakout=,first_breakout_tool=,first_breakout_machine=,second_breakout=,second_breakout_tool=,second_breakout_machine=,third_breakout=,third_breakout_tool=,third_breakout_machine=,repressing=,repressing_tool=,repressing_machine=,punching=Yes,punching_tool=Yes,punching_machine=5TO,punching_strips=Yes,punching_strips_size=,punching_strips_tool=Yes,punching_strips_machine=5TO,punching_single=,punching_single_size=,punching_single_tool=,punching_single_machine=,emboss=,emboss_tool=,emboss_machine=,dished=,dished_tool=,dished_machine=,bombe=,bombe_tool=,bombe_machine=,wave=,wave_tool=,wave_machine=,centre_hole=,centre_hole_tool=,centre_hole_machine=,polishing=,degreasing=,annealing=,scouring=Yes,notes=durchbrechen 5+1 Ringe / 1 Ring oben / 5 Ringe unten",1,0.0000,1,,,1,,1,,1,1,,1,1,1,,1,,,0,,,,,,,"/P06342-oR-Alt-Gold.jpg,/P06342-oR-Alt-Messing.jpg,/P06342-oR-Alt-Silver.jpg,/P06342-oR-Glanz-Gold.jpg,/P06342-oR-Glanz-Gun Metal.jpg,/P06342-oR-Glanz-Platin.jpg,/P06342-oR-Glanz-Raw.jpg,/P06342-oR-Glanz-Rose Gold.jpg,/P06342-oR-Glanz-Silver.jpg",,,,,,,,
This is my first import.
Second was only with 2 additional images.
Hi, @hostep this issue still exists in Magento 2.3.5-p1 鈥斅營 was able to replicate it.
edit: all that those changed did was break all the PRs and patches ;)
edit2: I'm using this PR with success now: https://github.com/magento/magento2/pull/26713
2.3.5-p1 duplicate images - issue not fixed
@magento I'm working on this
2.3.4 duplicate images - issue not fixed
It does not work for most of us because we have in our import CSV the base name of the image, for example;
base_image_78989.jpg
but magento's existing images are named /b/a/base_image_78989_10.jpg
. Unless there is a comparison between these two names on updating the product, this issue will persist.
@PsmIndia @jalung
Check PR https://github.com/magento/magento2/pull/26713
The PR comes with image hash compare logic, which is working fine (in my workflow) for the community version.
I can confirm #26713 is working fine on 2.3.5-p1. Since this seems to be an endless issue, here the link to the correct application of this patch via composer: https://devdocs.magento.com/guides/v2.4/comp-mgr/patching.html
I noticed that the PR has another issue: If you import data for another store (e.g. english translations) without any image related data in the import CSV, all images get removed (globally). Images should only be removed if the according image columns are present and empty.
How can I get this patch. Anyone please advise.
Get the patch corresponding to the pull request : https://patch-diff.githubusercontent.com/raw/magento/magento2/pull/26713.patch
And apply it to magento/catalog-import-export
composer dependency using https://github.com/cweagans/composer-patches
Hello,
I was trying to install the patch but getting errors. Magento unable to apply this patch.
Any one please advise. Attaching the screenshot.
Hello,
I was trying to install the patch but getting errors. Magento unable to apply this patch.Any one please advise. Attaching the screenshot.
See https://github.com/magento/magento2/issues/21885#issuecomment-644777537 for detailed instructions how to apply the patch
Try removing the PATCH [2/4] part entirely, and the last part about tests.
Hi Yonn,
Its a very long file. Could you please help me telling which line to which line.
Thanks.
So sad to see @magento-admin ignoring this...
We also need this resolving, was hopeful 2.4 would have brought some fresh updates but at present we have to drop all images before importing otherwise end up with new images duplicated, empty and set as base, small and thumb :-(
Can anyone confirm this happens with API imports as well? We upgraded from 2.3.3 to 2.3.5 and noticed various images being duplicated. It seems that's related to the number oft store views. basically for every store view a new copy.
1. Edit this file:
/vendor/magento/module-catalog-import-export/Model/Import/Uploader.php:134
2. Modify init() function:
Change line 134 in the init() function from
$this->setAllowRenameFiles(true);
to
$this->setAllowRenameFiles(false);
Result:
This will replace the product images instead of just renaming and duplicating them.
Keep in mind:
This is just a temporary solution and with the next magento-update you have to do this again.
To make the changes permanent you have to create a module that overrides this file.
This is my first comment on github. So please be kind with your feedback - thanks!
For the Magento dev team:
/vendor/magento/module-catalog-import-export/Model/Import/Uploader.php
getNewFileName()
and just renameing it if the filesize has changed.:white_check_mark: Confirmed by @engcom-Alfa
Thank you for verifying the issue. Based on the provided information internal tickets MC-38267
were created
Issue Available: @engcom-Alfa, _You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself._
Hi @simonworkhouse. Thank you for your report.
The issue has been fixed in magento/magento2#26713 by @PieterCappelle in 2.4-develop branch
Related commit(s):
The fix will be available with the upcoming 2.4.2 release.
Most helpful comment
Hi, @hostep this issue still exists in Magento 2.3.5-p1 鈥斅營 was able to replicate it.
edit: all that those changed did was break all the PRs and patches ;)
edit2: I'm using this PR with success now: https://github.com/magento/magento2/pull/26713