Add main image, additional image magento products pro-grammatically

This tutorial I am explaining how to add main image, additional image magento products pro-grammatically. The following add product images to the magento products programmatically.

Here I am importing the data from the products.csv and get the product data from the CSV . Please click here to find how to read the data from the CSV or see the related links below.

Add main image, additional image magento products pro-grammatically

Step by step explanation

  1. Add Main image
  2. Add additional images

Set default image

 $importDir = Mage::getBaseDir('media') . DS;
 
 // main image
        if ($mainImage) {
            $image_directory = $dir . DS . 'data' . DS . $mainImage;
            if (file_exists($image_directory)) {
                $product->setMediaGallery(array('images' => array(), 'values' => array()))//media gallery initialization
                ->addImageToMediaGallery($image_directory, array('image', 'thumbnail', 'small_image'), false, false);//assigning image, thumb and small image to media gallery

            } else {
                $image_directory = $dir . 'data' . DS . 'comingsoon.jpg';
                $product->setMediaGallery(array('images' => array(), 'values' => array()))//media gallery initialization
                ->addImageToMediaGallery($image_directory, array('image', 'thumbnail', 'small_image'), false, false);
            }
        }

Add additional images

The $import_product[29] is an array of images separated by commas.

$importDir = Mage::getBaseDir('media') . DS;
 // additional images
        if ($import_product[29] != '') {
            $addImages = explode(",", trim($import_product[29]));
            foreach ($addImages as $additional_image) {
                $image_directory = $dir .DS.'data'.DS. trim($additional_image);
                if (file_exists($image_directory)) {
                    $product->addImageToMediaGallery($image_directory, null, false, false);
                } else {
                    $image_directory = $dir . 'data' . DS . 'comingsoon.jpg';
                    $product->addImageToMediaGallery($image_directory, null, false, false);
                }
            }
            echo 'Additional images for product ' . $product->getName() . ' ' . $product->getId() . ' imported successfully' . PHP_EOL;
        }

import-additional-images

Share this Tutorial

4 comments on “Add main image, additional image magento products pro-grammatically

  1. Jinal Kothari on

    Sorry to say but its still not working for me. I am able to download the images but not able to set it while uplaoding. Please help ….

    Reply
  2. Jinal on

    hi,
    Please help me with my 2 issues:

    1. when i insert main image it is visible in backend but the radio buttons for the image are not ticked.
    2. the code which you have suggested here, is not working for me. I am downloading image from external link and when i echoed the name & path, it is half taken only.

    Please advice me what to do. I am a beginner.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*