cancel
Showing results for 
Search instead for 
Did you mean: 

How can I replace product images with new images using excel import in Magento 2.3

SOLVED

How can I replace product images with new images using excel import in Magento 2.3

I want to update only the product images of some products. I created csv sheet which contains the sku of the products and 

base_image, small_image, thumbnail_image, swatch_image ,additional_images

After importing all the images are uploaded successfully. But my issue is that, the old images are exists for these products. Please tell me the method to replace all the product images of the products at the time of excel importing.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: How can I replace product images with new images using excel import in Magento 2.3

Hi @sreerag_as 

 

True, I Will share if i found anything on the same.

 

Happy to help and keep helping others Smiley Happy

 

if issue solved,Click Kudos & Accept as Solution

View solution in original post

8 REPLIES 8

Re: How can I replace product images with new images using excel import in Magento 2.3

Hi @sreerag_as 

 

I understand the problem you are facing !

 

But as known as i know - as of now there is no pre-defined solution for that this issue is known issue and already reported on github as well, here is the link - https://github.com/magento/magento2/issues/14398

 

For more details refer this link as well - https://magento.stackexchange.com/questions/199182/magento-2-replace-product-images-via-csv-import

 

So as of now you will require to remove it manually and then assign the new images by CSV import.

 

Hope it helps !

if issue solved,Click Kudos & Accept as Solution

Re: How can I replace product images with new images using excel import in Magento 2.3

@Manthan Dave Thank you for your help. So I have to remove it manually. It is very tedious task, since we have 5000 products.

Re: How can I replace product images with new images using excel import in Magento 2.3

Hi @sreerag_as 

 

True, But looking at all the possible solution - there is no solution found for this problem as of now.

 

So you have to do it manually.

 

Hope it helps !

if issue solved,Click Kudos & Accept as Solution

Re: How can I replace product images with new images using excel import in Magento 2.3

@Manthan Dave Ok. Thank you for your reply.

 

Re: How can I replace product images with new images using excel import in Magento 2.3

Hi @sreerag_as 

 

True, I Will share if i found anything on the same.

 

Happy to help and keep helping others Smiley Happy

 

if issue solved,Click Kudos & Accept as Solution

Re: How can I replace product images with new images using excel import in Magento 2.3

Hi,

I wanted to check back in on this topic.

Has a better solution been found for this? The manual solution is not a feasible one for our needs.

Re: How can I replace product images with new images using excel import in Magento 2.3

Adding and Removing Media Images Programmatically in Magento 2

This post will describe the process of adding and removing media images programmatically in Magento 2 using the Magento_Catalog module classes. Usually when developers need to programmatically add and remove product images, it requires a one-off script to be run. Therefore the code shown will reside in a single file that externally bootstraps the Magento 2 application

The code below has been tested as of Magento Open Source version 2.2.5 and will remove all existing gallery images from a product, then add a single image, assigning the image to the base, small_image and thumbnail.

There are a few requirements as pointed out in the code comments.

  • The document root of the Magento 2 application must be set to ROOT/pub.
  • The directory containing the images should be place within ROOT/pub/media.
  • The images are named identical to the SKU of the product which images need to be updated.
  • Ensure that [image_dir] is replaced by the name of the directory which contains the images.

Add a file within the ROOT/pub directory containing the code below and run the code either within the browser or via CLI. You can modify various aspects of the file (such as the glob’d image file types) to suit your needs.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
 
/*
 * Assumes doc root is set to ROOT/pub
 */
require_once dirname(__DIR__) . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
 
class AssignImages extends \Magento\Framework\App\Http implements \Magento\Framework\AppInterface
{
    public function launch()
    {
        $state = $this->_objectManager->get('Magento\Framework\App\State');
        $state->setAreaCode('adminhtml');
 
        $galleryReadHandler = $this->_objectManager->create('Magento\Catalog\Model\Product\Gallery\ReadHandler');
        $imageProcessor = $this->_objectManager->create('Magento\Catalog\Model\Product\Gallery\Processor');
        $productGallery = $this->_objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Gallery');
 
        /**
         * Assumed images are named [sku].[ext] and reside in ROOT/pub/media/[image_dir]
         */
        foreach (glob(__DIR__ . "/media/[image_dir]/*.{jpg,png,gif}", GLOB_BRACE) as $image) {
            $imageFileName = trim(pathinfo($image)['filename']);
            $sku = $imageFileName;
            try {
                $product = $this->_objectManager->create('Magento\Catalog\Model\Product')->loadByAttribute('sku', $sku);
                if ($product) {
                    $galleryReadHandler->execute($product);
 
                    // Unset existing images
                    $images = $product->getMediaGalleryImages();
                    foreach($images as $child) {
                        $productGallery->deleteGallery($child->getValueId());
                        $imageProcessor->removeImage($product, $child->getFile());
                    }
 
                    /**
                     * Add image. Image directory must be in ROOT/pub/media for addImageToMediaGallery() method to work
                     */
                    $product->addImageToMediaGallery('[image_dir]' . DIRECTORY_SEPARATOR . pathinfo($image)['basename'], array('image', 'small_image', 'thumbnail'), false, false);
                    $product->save();
                    echo "Added media image for {$sku}" . "\n";
                }
            } catch (\Exception $e) {
                echo $e->getMessage();
            }
        }
        return $this->_response;
    }
 
    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
    {
        echo $exception->getMessage();
        return false;
    }
}
 
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('AssignImages');
$bootstrap->run($app);

Re: How can I replace product images with new images using excel import in Magento 2.3

I don't understand how and why this is marked as the solved. It looks like a solutions was provided for the problem but in fact there is none. Can someone clarify why was the answer selected as the solution.. and most importantly, is there a real solution for this?
Thanks