I am updating product's attributes and images through program. The problem is that, it is adding new images everytime and appending _1
or _1_1
to the file. Hence a lot of duplicate files in the drive.
I searched in the forum for the solution. They say not to save the product twice. You can see below code that I have a function downloadAndImportImage()
that adds images and later on I save the product.
What am I missing ? Thanks in advance.
function downloadAndImportImage($image_url, $_product, $num, $sku){
try{
.
.
.
if (file_exists($filepath_to_image)) {
$_product->addImageToMediaGallery($filepath_to_image, array('image', 'small_image', 'thumbnail'), false, false);
}
}catch(Exception $e){
echo $e->getMessage();
}
}
function updateProduct(){
...
if($record[14]){
if ($record[14] != $record[13]){
downloadAndImportImage($record[14], $_product, 14, $record[0]);
}
}
if($record[15]){
if ($record[14] != $record[15]){
downloadAndImportImage($record[15], $_product, 15, $record[0]);
}
}
$_product->save();
}