I can add an image to a product during creating using the following script. However, I'm having trouble loading a product and add additional image to the product media gallery. Do I have to delete the product media gallery and readd all the images at once? It's one image per row in a csv file.
public function addImage($product, $image)
{
$imagePath = $this->downloadImage($image);
$product->setMediaGallery(array('images' => array(), 'values' => array()));
if (is_file($imagePath)) {
$product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), false, false);
}
}
Why are you using a script to add images? Adding images in magento can be easily done via the admin interface.
Just login to the admin panel, Catalog > Manage Products > Edit Product > Images (on left panel).
You add the additional images using the following script.
$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;
}
The $import_product[29] is an array of images separated by commas. Please refer my tutorial as well which explains how to set default image as well.