3

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?

2 Answers 2

3

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

4
  • 1
    You should say that you saved the product two times or something like that, then mark this answer as the answer. Commented Apr 17, 2014 at 14:30
  • I can mark it as answered after 2 days ;) Commented Apr 17, 2014 at 14:33
  • I have the same exact problem that you have but I'm only saving the product once was your function a custom self made function? Commented May 4, 2017 at 21:03
  • Yes it is from a custom self build import script. I had the problem that an other function was saving te product also. But if you just save the product only once at the end of the import it should be good, otherwise it is not related to this problem I think. Commented May 5, 2017 at 7:26
0

Have a look at the addImageToMediaGallery function in app/code/core/Mage/Catalog/Model/Product.php:

/** * Add image to media gallery * * @param string $file file path of image in file system * @param string|array $mediaAttribute code of attribute with type 'media_image', * leave blank if image should be only in gallery * @param boolean $move if true, it will move source file * @param boolean $exclude mark image as disabled in product page view * @return Mage_Catalog_Model_Product */ public function addImageToMediaGallery($file, $mediaAttribute=null, $move=false, $exclude=true) { $attributes = $this->getTypeInstance(true)->getSetAttributes($this); if (!isset($attributes['media_gallery'])) { return $this; } $mediaGalleryAttribute = $attributes['media_gallery']; /* @var $mediaGalleryAttribute Mage_Catalog_Model_Resource_Eav_Attribute */ $mediaGalleryAttribute->getBackend()->addImage($this, $file, $mediaAttribute, $move, $exclude); return $this; } 

You'll see there is another parameter which is a boolean to exclude the product from the small image preview, yet you're setting this boolean as False. The primary image should have this value set to 'true' to avoid duplicating the image. So give that a shot:

$this->product->addImageToMediaGallery( $tmpFile, array('image', 'thumbnail', 'small_image'), true); 

Then you should be all set.

8
  • That doesn't work any other suggestions? Commented Apr 17, 2014 at 12:38
  • You may want to unselect it as the correct answer then :-P. What was the outcome of the code change? Commented Apr 17, 2014 at 12:42
  • Sorry i clicked accidentaly on the button ;). I didn't noticed any changes in the backend Commented Apr 17, 2014 at 12:49
  • You're not running the Magento Compiler right? If so, you'd need to recompile. Other than that you may need to refresh your cache for the changes to take effect. Commented Apr 17, 2014 at 12:50
  • Compiler and cache are both disabled. I also know that when I imported the first time (3 months ago) it worked. Today I deleted all the products with $product->delete() because I wanted to import all the products as new product. And now I have the problem with the images. And I didn´t change any coding Commented Apr 17, 2014 at 12:53

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.