Skip to content
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

Product images are being duplicated on import #21885

Open
simonworkhouse opened this issue Mar 22, 2019 · 41 comments
Open

Product images are being duplicated on import #21885

simonworkhouse opened this issue Mar 22, 2019 · 41 comments

Comments

@simonworkhouse
Copy link

@simonworkhouse simonworkhouse commented Mar 22, 2019

When importing product data the product images are being duplicated for each import run.

Preconditions

  1. Magento 2.3.0
  2. PHP 7.1.27
  3. MySQL 5.7.25

Steps to reproduce

  1. composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition <install dir>
  2. cd <install dir>
  3. ./bin/magento setup:install --backend-frontname=? --db-host=? --db-name=? --db-user=? --db-password=? --base-url=? --admin-user=? --admin-password=? --admin-email=? --admin-firstname=? --admin-lastname=?
  4. mkdir var/import-images
  5. Add images 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.
  6. Import the product CSV data below twice
"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",,

Import settings

  • Entity Type: Products
  • Import Behavior: Add/Update
  • Stop on Error (default)
  • Allowed Errors Count: 10 (default)
  • Field separator: , (default)
  • Multiple value separator: , (default)
  • Empty attribute value constant: __EMPTY__VALUE__ (default)
  • Fields Enclosure: Not checked (default)
  • Images File Directory: var/import-images

Expected result

Products end up with each product image only attached once.

Actual result

Products end up with all product images duplicated.

@magento-engcom-team

This comment has been minimized.

Copy link
Contributor

@magento-engcom-team magento-engcom-team commented Mar 22, 2019

Hi @simonworkhouse. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • Summary of the issue
  • Information on your environment
  • Steps to reproduce
  • Expected and actual results

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?

  • yes
  • no
@engcom-backlog-nazar engcom-backlog-nazar self-assigned this Mar 22, 2019
@magento-engcom-team

This comment has been minimized.

Copy link
Contributor

@magento-engcom-team magento-engcom-team commented Mar 22, 2019

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: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label 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 branch

    Details- 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.

@dipti2jcommerce

This comment has been minimized.

Copy link

@dipti2jcommerce dipti2jcommerce commented Mar 22, 2019

@magento-engcom-team give me 2.3-develop instance

@magento-engcom-team

This comment has been minimized.

Copy link
Contributor

@magento-engcom-team magento-engcom-team commented Mar 22, 2019

Hi @dipti2jcommerce. Thank you for your request. I'm working on Magento 2.3-develop instance for you

@magento-engcom-team

This comment has been minimized.

Copy link
Contributor

@magento-engcom-team magento-engcom-team commented Mar 22, 2019

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.

@magento-engcom-team

This comment has been minimized.

Copy link
Contributor

@magento-engcom-team magento-engcom-team commented Mar 22, 2019

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.

@magento-engcom-team magento-engcom-team added this to Ready for QA in Community Backlog Mar 27, 2019
@m2-backlog m2-backlog bot moved this from Ready for QA to Ready for Dev in Community Backlog Mar 27, 2019
@magento-engcom-team magento-engcom-team removed this from Ready for Dev in Community Backlog Mar 28, 2019
@magento-engcom-team magento-engcom-team added this to Ready for QA in Community Backlog Mar 28, 2019
@m2-backlog m2-backlog bot moved this from Ready for QA to Ready for Dev in Community Backlog Mar 28, 2019
@tnsezer

This comment has been minimized.

Copy link

@tnsezer tnsezer commented Mar 28, 2019

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.

@dmanners

This comment has been minimized.

Copy link
Contributor

@dmanners dmanners commented Apr 4, 2019

Thank you for the information @tnsezer

@erfanimani

This comment has been minimized.

Copy link
Contributor

@erfanimani erfanimani commented Apr 11, 2019

Duplicate of #14398

See PR here: #21146

@erfanimani

This comment has been minimized.

Copy link
Contributor

@erfanimani erfanimani commented Apr 11, 2019

Actually, there's a better PR here, based on mine but it also deals with image deletion: #21855

@dmanners

This comment has been minimized.

Copy link
Contributor

@dmanners dmanners commented Apr 11, 2019

@erfanimani thanks for the update. Are the issues duplicated or the PRs?

@erfanimani

This comment has been minimized.

Copy link
Contributor

@erfanimani erfanimani commented Apr 11, 2019

@dmanners Both actually — this 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):

@soundararajanm1990

This comment has been minimized.

Copy link

@soundararajanm1990 soundararajanm1990 commented May 4, 2019

I am working on this at #dmcdindia1

@magento-engcom-team

This comment has been minimized.

Copy link
Contributor

@magento-engcom-team magento-engcom-team commented May 4, 2019

@soundararajanm1990 thank you for joining. Please accept team invitation here and self-assign the issue.

@m2-assistant

This comment has been minimized.

Copy link

@m2-assistant m2-assistant bot commented May 4, 2019

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:

  • 1. Add/Edit 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 branch

    Details- 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.

@m2-backlog m2-backlog bot moved this from Ready for Dev to Dev in Progress in Community Backlog May 4, 2019
@yogeshkhasturi

This comment has been minimized.

Copy link

@yogeshkhasturi yogeshkhasturi commented May 30, 2019

Hi @soundararajanm1990,

Have you fixed this issue? its also coming when trying to Duplicate product.

Thanks
Yogesh

@duckchip

This comment has been minimized.

Copy link
Contributor

@duckchip duckchip commented Jun 7, 2019

what's the status on this issue?

@soundararajanm1990

This comment has been minimized.

Copy link

@soundararajanm1990 soundararajanm1990 commented Aug 9, 2019

We can't solve this issue. Still now pending.

@Beagon

This comment has been minimized.

Copy link
Contributor

@Beagon Beagon commented Aug 22, 2019

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?

@soundararajanm1990 soundararajanm1990 removed their assignment Aug 23, 2019
@m2-backlog m2-backlog bot moved this from Dev in Progress to Ready for Dev in Community Backlog Aug 23, 2019
@kevinvuillemin

This comment has been minimized.

Copy link

@kevinvuillemin kevinvuillemin commented Aug 28, 2019

I have same problem, magento2.3.2 duplicated images after import product csv.

Did you find a solution ?

@erfanimani

This comment has been minimized.

Copy link
Contributor

@erfanimani erfanimani commented Aug 28, 2019

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..

@Beagon

This comment has been minimized.

Copy link
Contributor

@Beagon Beagon commented Aug 29, 2019

@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.

@kevinvuillemin

This comment has been minimized.

Copy link

@kevinvuillemin kevinvuillemin commented Aug 29, 2019

Thank you @erfanimani

With a combination of the solutions, the import works very well.

@MackieeE

This comment has been minimized.

Copy link

@MackieeE MackieeE commented Aug 29, 2019

+1 Hoping to see this merged in soon, big help this :)

@skapin

This comment has been minimized.

Copy link

@skapin skapin commented Sep 4, 2019

Wow same trouble for us... This is awsome magento let this kind of BUG in a 2.3.2 version released worldwild...

@skapin

This comment has been minimized.

Copy link

@skapin skapin commented Sep 4, 2019

@kevinvuillemin can you share the working solution ? I'm strugeling with all the PR closed and open speaking around it. Thank you in advance

@kevinvuillemin

This comment has been minimized.

Copy link

@kevinvuillemin kevinvuillemin commented Sep 4, 2019

@skapin, of course ! how do you want me to share the file?

@skapin

This comment has been minimized.

Copy link

@skapin skapin commented Sep 4, 2019

@kevinvuillemin you can send me a mail to skapinthefourb gmailcom address for example, or create a repository

As you want. Thank you so much :)

@kevinvuillemin

This comment has been minimized.

@skapin

This comment has been minimized.

Copy link

@skapin skapin commented Sep 5, 2019

@kevinvuillemin It works on the first try. Copy/paste instead of Product.php and voila !
Magento version : 2.3.2

@kevinvuillemin

This comment has been minimized.

Copy link

@kevinvuillemin kevinvuillemin commented Sep 5, 2019

Et voila !
Au plaisir ;-)

@soundararajanm1990

This comment has been minimized.

Copy link

@soundararajanm1990 soundararajanm1990 commented Sep 10, 2019

@kevinvuillemin Your code not working in Magento version : 2.3.1

@kevinvuillemin

This comment has been minimized.

Copy link

@kevinvuillemin kevinvuillemin commented Sep 10, 2019

Working with 2.3.2 for me, are you sure ?

@soundararajanm1990

This comment has been minimized.

Copy link

@soundararajanm1990 soundararajanm1990 commented Sep 10, 2019

yes we used 2.3.1 not working

@skapin

This comment has been minimized.

Copy link

@skapin skapin commented Sep 10, 2019

@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)

@MackieeE

This comment has been minimized.

Copy link

@MackieeE MackieeE commented Sep 13, 2019

@kevinvuillemin They moved the issue #14398 onto a new phase - does this mean your PR is ready for Testing on the next Dev version?

@ahmadwaliesipick

This comment has been minimized.

Copy link

@ahmadwaliesipick ahmadwaliesipick commented Sep 30, 2019

any update on this issue?

@Yonn-Trimoreau

This comment has been minimized.

Copy link

@Yonn-Trimoreau Yonn-Trimoreau commented Oct 4, 2019

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);
    }
}
@MackieeE

This comment has been minimized.

Copy link

@MackieeE MackieeE commented Oct 19, 2019

Well sadly this looks like it missed the 2.3.3 ship :(

@kevinvuillemin

This comment has been minimized.

Copy link

@kevinvuillemin kevinvuillemin commented Oct 19, 2019

Realy ? That’s not cool.

But This fix working ? :

https://gist.github.com/kevinvuillemin/802cc5e4b476e73c1c5838b143bd6a51

@simonworkhouse

This comment has been minimized.

Copy link
Author

@simonworkhouse simonworkhouse commented Oct 21, 2019

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 7803eed

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.