Magento: Import Multiple Images or Remove Images durring Batch Import
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.

19 Responses to “ Magento: Import Multiple Images or Remove Images durring Batch Import ”
March 23rd, 2010 at 9:48 am
Hi Mike,
Thanks for sharing this code. It’s made my life much easier. However I’m having a problem with it that I just can’t figure out. The problem is specifically related to the delete images chunk of code.
Your code is in the proper location in my script and I’ve made no modifications to it. I am using a modified import script though, which allows for the uploading of configurable products.
When the delete images column is set to true, everything gets imported but the configurable attribute on my configurable products does not get set. If I set delete_all_images to ‘no’ the problem is resolved.
Looking through your code I can’t figure out why this would be happening, it’s not making any sense to me, and my head is starting to hurt almost as bad as my keyboard it keeps hitting.
If you’ve heard of this issue or have any off-the-top-of-your-head suggestions please share, but please don’t go out of your way.
Thanks,
James
March 23rd, 2010 at 10:08 am
Hmm… I am not sure what would be causing the Remove Images functionality to be affecting any other part of the import process. A work around would be to import the images then remove the image fields from the import spreadsheet and re-import the data again. this way you don’t need to use the remove images functionality. I only used it so I did not need to remove the image from the spreadsheet if I adjusted any information and had to re-import it.
April 8th, 2010 at 4:23 am
Hi Mike,
I could use some help here. I’m trying to make this work and have added your codes into my Product.php but getting this error during import.
Parse error: syntax error, unexpected T_PUBLIC in /home/user/public_html/app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php on line 776
my line 776 is this:
public function saveRowSilently(array $importData)
April 8th, 2010 at 4:34 am
Sorry, I forgot to include my version which is 1.3.2.3
April 8th, 2010 at 6:11 am
It looks like you probably did not close a function above this one. I believe this error is saying that PHP was not expecting a function to be declared. Your Error will be in the code above line 776.
May 13th, 2010 at 7:31 pm
I am getting this error in Magento 1.4.0.1
Warning: Invalid argument supplied for foreach() in /home/folder/public_html/app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php on line 694
line 694 is in the Remove Image section:
foreach($galleryData['images'] as $image){
Any ideas of the source of the problem?
Thanks,
Chris
May 14th, 2010 at 7:03 am
Try doing a
print_($galleryData);and see if $galleryData['images'] is in the array. I have not used this on Magento 1.4.x and they might have changed things a little.Let me know whats in the array and we can proceed from there.
May 15th, 2010 at 3:54 am
Hi Mike,
Thanks for the share, your code helps me fixing this problem of duplicate image (my version is 1.4.0.1).
I have added a little line because magento keeps adding a new picture in the catalog/product folder and change the picture’s name each time you import again the product so you have to remove directly the picture :
if ($gallery->getBackend()->getImage($product, $image['file'])) {
$gallery->getBackend()->removeImage($product, $image['file']);
unlink (Mage::getBaseDir('media') . DS . 'catalog' . DS . 'product' . $image['file']);
}
June 11th, 2010 at 3:10 pm
One more thing. Image imports always trip me up by requiring a / before the filename. All you have to do is add the following lines to your code after the try { in code sample number 3. This adds a / to your image file name if it’s not already there.
// if the leading / was forgotten then add it
if (substr($file, 0, 1) '/') {
$file = '/'.$file;
}
This will make the whole foreach loop look like this -
foreach ($imageData as $file => $fields) {
try {
// if the leading / was forgotten then add it
if (substr($file, 0, 1) '/') {
$file = '/'.$file;
}
$product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . $file, $fields);
}
catch (Exception $e) {}
}
Also – remember that the images you want to import go into the media/import directory not the var/import
Cheers
Greg
June 30th, 2010 at 10:27 am
Hi Mike,
Thanks for your good job.
I am using magento 1.4.1.0. When I import data from csv file by your code, images imported successfully but images are not setting as “thumbnail”, “image” or “small_image”.
can you tell me why? I have searched to resolve this problem but failed.
Can you resolve this problem?
Regards,
Qasim
June 30th, 2010 at 10:44 am
@Qasim – Hey It has been a while since I have used this script. In the cvs file you should have images in each column that you want to set(ex: image, thumbnail, …).
Create a product using Magento with images then export this product(this will give you a base csv file to work with) and make sure that your fields are named the same when importing.
I have not tested this in Newer versions of Magento, but I don’t see why any thing related to this would have changed.
July 4th, 2010 at 6:28 pm
I am use 1.4.1.0 now,
when i upload batch as before 1.4.0.1,
it is show error now:
———————————
Starting profile execution, please wait…
Warning: Please do not close the window during importing/exporting data
Starting Mage_Dataflow_Model_Convert_Parser_Csv :: parse
Method “parse” not defined in adapter catalog/convert_adapter_product.
Please wait while the indexes are being refreshed.
Finished profile execution.
———————————
Any good idea?
July 4th, 2010 at 6:38 pm
The only thing I can think of is that you need to re-index everything, then try again.
July 8th, 2010 at 8:47 am
Hello,
I am very excited about this code, but when I perform the import I am getting this error:
Notice: Undefined property: Mage_Catalog_Model_Convert_Adapter_Product::$_inventorySimpleFields in /home/wwwfore7/public_html/friend/app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php on line 616
Any ideas?
Thanks again.
July 8th, 2010 at 9:23 am
Also, if i let the import run with the notice, I get this at the very end about 10 times:
Parse error: syntax error, unexpected T_IF, expecting T_FUNCTION in /home/wwwfore7/public_html/friend/app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php on line 664
July 12th, 2010 at 11:02 pm
@Laura:
RE – Mage_Catalog_Model_Convert_Adapter_Product::$_inventorySimpleFields:
1. Did you edit the core file or override the core file?
2. What version are you using?
3. Does the
3. Please post a line or two above and below the line the error was on.
RE – Parse error: syntax error, unexpected T_IF, expecting T_FUNCTION:
This sounds like your braces are not closed right. Check your code.
July 14th, 2010 at 12:16 am
hi,
can i import product from RSS FEED of other websites ..?
July 14th, 2010 at 4:50 am
I am not sure.I have never tried it. I would assume that if the feed has all the required data and you had the attributes and attribute set already set up you could.
July 14th, 2010 at 7:57 pm
I just wanted to follow up a bit from my first post. Here is my first post:
Well I found out that you cannot have the “remove_all_images” column in your first import, you must have images for your products first.
Also, I found that instead of editing the core file, it is a lot safer to go ahead and make a local file ( /public_html/app/code/local/Mage/Catalog/Model/Convert/Adapter/Product.php)Then if it doesn’t work just delete it.
Thanks again Mike…I seemed to have ironed out all my problems.
Chris