In this article I want to show you guys how to import product images in Magento 2. The snippet is part of a bigger class, however it should be pretty straight forward to understand. Otherwise, leave a message and I’m more than happy to explain it.

The method below looks for images in the folder “<MAGENTO_DIR>/pub/media/import/” and, if found, they get imported for the product.

You will also notice the WHILE, this loop searches for a sequence of 10 images per product.

/**
 * @param \Magento\Catalog\Model\Product $product
 * @return $this
 */
protected function importImages(\Magento\Catalog\Model\Product $product)
{
    $imageName = $product->getSku();
    $imageFolder = strtolower(explode('_', $imageName)[0]) . "/";

    $dir = $this->_objectManager->get('\Magento\Framework\App\Filesystem\DirectoryList');
    $mediaImportFolder = $dir->getPath(DirectoryList::MEDIA);

    if($this->isSetFlag('remove_existing_images')) {
        // remove images
        $existingMediaGalleryEntries = $product->getMediaGalleryEntries();
        foreach ($existingMediaGalleryEntries as $key => $entry) {
            unset($existingMediaGalleryEntries[$key]);
        }
        $product->setMediaGalleryEntries($existingMediaGalleryEntries);
        $this->_productRepositoryInterface->save($product);
    }

    $product->setMediaGallery(array('images'=>array (), 'values'=>array ()));

    $i = $foundImages = 0;
    while($i < 10) {
        $i++;

        $imagePath = 'import/' . $imageFolder . $imageName . "_IMAGE" . $i . ".jpg";
        $imageAbsolutePath = $mediaImportFolder . '/' . $imagePath;

        if(!$imageAbsolutePath = $this->fileExists($imageAbsolutePath)) {
            continue 1;
        }

        $flags = ($foundImages == 0) ? ['image','thumbnail','small_image'] : [];

        $product->addImageToMediaGallery($imageAbsolutePath, $flags, false, false);

        $this->log(" - Image {$imagePath} imported with success for the product SKU {$product->getSku()}");
        $foundImages++;
    }

    if($foundImages == 0) {
        $this->log(" - No images found for the product SKU {$product->getSku()}");
    }

    return $this;
}

Troubleshooting

Keep an eye on your log. If for any reason you do not get the images imported it might be relate to the following errors:

“Error: Required parameter ‘theme_dir’ was not passed”.

To fix it, just get sure you have a Theme defined for your installation.

 

Screen Shot 2016-08-08 at 11.26.14 AM

Error: URL key for specified store already exists.

Delete all the records in url_rewrite table to fix this issue:

mysql> delete  from url_rewrite where entity_type = 'product';

Happy Coding!

Magento 2: Import Product Images Programmaticallyhttps://i1.wp.com/blog.mdnsolutions.com/wp-content/uploads/2016/05/Screen-Shot-2016-05-06-at-9.29.33-AM.png?fit=452%2C240&ssl=1https://i1.wp.com/blog.mdnsolutions.com/wp-content/uploads/2016/05/Screen-Shot-2016-05-06-at-9.29.33-AM.png?resize=150%2C150&ssl=1Renato MedinaMagentoMagento 2Images,Import,Magento 2,Product,ProgrammaticalyIn this article I want to show you guys how to import product images in Magento 2. The snippet is part of a bigger class, however it should be pretty straight forward to understand. Otherwise, leave a message and I'm more than happy to explain it. The method below looks...A Magento Craftsman in Melbourne