Magento 1.7 Product – Save Product Duplicates the Image

adminimageimportmagento-1.7product

I have an import script for saving new products.

To save the image I use:

<?php
Mage::init();
$app = Mage::app('default');

[...lots of code...]

$tmpFile = 'tmp/' . $this->productInfo['SKU'] . '.jpg';
$this->product->addImageToMediaGallery( $tmpFile, array('image', 'thumbnail', 'small_image'), false );
$this->product->save();
?>

The product is added to Magento.

When I go to the images, I will see 2 images the first one works and the second one doesn't work and has the same image name but with _2.jpg. Also the second broken image is set as image, thumbnail and small image.

Why does it add two images?

Best Answer

I have fixed the problem :)

I call a function. Inside this function I save the product.

On the next line after calling the function I save the product again ( so I saved the product two times )

It looks like both saves run at the same time because I deleted the save function from my custom function and now it works like it should :D