This article will detail how to adjust the Magento Batch Import (Dataflow) to allow uploading multiple gallery images, and also how to add a way to remove all images during Import/Update.

These are the accompaning files: (Please reference this file if you have any quetion about the code examples below. The WYSIWYG editor was messing things up. mainly “&’s” and “empty()’s” )
1. Full Code Example

First we are going to have to modify the the adapter used to save/update products during the import process. The file used is located here: “/app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php“. Since we are going to be modifying this file we should extend it so we don’t change any core files. There are plenty of articles that detail how to extend/customize Magento files so I will not go into it.

First lets tackle adding multiple gallery images. These are images that appear on the products detail page below the main image. We will be making all our edits in the Product.php file mentioned in the previous paragraph. Find the SaveRow method. In this method find the code where Magento saves the products images(should be similar to the following code and around line 630).

        $imageData = array();
        foreach ($this->_imageFields as $field) {
            if (!empty($importData[$field]) && $importData[$field] != 'no_selection') {
                if (!isset($imageData[$importData[$field]])) {
                    $imageData[$importData[$field]] = array();
                }
                $imageData[$importData[$field]][] = $field;
            }
        }

        foreach ($imageData as $file => $fields) {
            try {
                $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . $file, $fields);
            }
            catch (Exception $e) {}
        }

First thing we will do is change the addImageToMediaGallery so that when it saves the image it is not excluded(saves you from having to re-edit the products after import to make the images appear.).


        foreach ($imageData as $file => $fields) {
            try {
            	// changed to not mark imported images as 'excluded' - see http://www.magentocommerce.com/boards/viewthread/6971/P15/ and http://www.magentocommerce.com/boards/viewthread/40007/
                //$product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . $file, $fields);
                $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . $file, $fields, false, false);
            }
            catch (Exception $e) {}
        }

Now we can add the code to import multiple gallery images. Just below the code above add this new code:

            //Now add each gallery image.
        if (!empty($importData['gallimg'])) {
			//$importData['gallimg'] should be a list of images to import seperated by ";"
			$images = explode(";", $importData['gallimg']);
			foreach($images as $image){
				//Don't add field for gallimg's
				try {
					//made second param null so it is not added as the main image, small or thumb. just added to the gallery
					$product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . trim($image), NULL, false, false);
				}
				catch (Exception $e) {}

			}
        }

In order to use this new code you need to add a new column to your import spreadsheet. Label the column “gallimg”. This needs to be a list of the images you want to import separated by a “;”.
Now that was not too hard was it?

Now we can address removing images during the import/update process. In order to remove all the images you will need to continue modifying the saveRow method. Add the following code just above the code we previously edited.

        //MRD added to remove all images for product before uploading new images
        if(isset($importData['remove_all_images']) && $importData['remove_all_images']=="yes" ){
	        //check if gallery attribute exists then remove all images if it exists
	        //Get products gallery attribute
	        $attributes = $product->getTypeInstance()->getSetAttributes();
        	if (isset($attributes['media_gallery'])) {
				$gallery = $attributes['media_gallery'];
				//Get the images
				$galleryData = $product->getMediaGallery();
				foreach($galleryData['images'] as $image){
				//If image exists
			    	if ($gallery->getBackend()->getImage($product, $image['file'])) {
			            $gallery->getBackend()->removeImage($product, $image['file']);
			        }
				}
			}
			#$gallery->clearMediaAttribute($product, array('image','small_image','thumbnail'));
        }
        //END Remove Images

To utilize the code we just created you will need to add the “remove_all_images” column to your spreadsheet and set the value to “yes”.

This was written using Magento version 1.2.1 so your files could be slightly different, but should still work if you follow the instruction. If you have any problems please leave a productive comment and I will see how I can help.