2

I am creating a simple product via code but struggling adding the images correctly. The product uploads fine and also the images too, which show ok on the frontend. But the radio buttons for base, small image and thumbnail are not ticked for the default value of the product. So the products image does not show up on the front end.

The code i am using to add the image is

$product->addImageToMediaGallery($filePath, array ('image','small_image','thumbnail'), false, false);

Magento Screenshot

This is a multisite store. How can i get the default values of the image to tick when the image upload is done?

Thanks for the help!!

asked Jul 14, 2013 at 19:23

1 Answer 1

10

Here's the code I used recently for creating a product. It's based on having an array of image urls ($imgArray) rather than a single image url. It adds the first image in the array as the thumb/base/small and the rest as additional images.

It was based on MagePsycho's blog post here;

http://www.blog.magepsycho.com/how-to-import-product-images-from-external-url-in-magento/

 // We set up a $count variable - the first image gets used as small, thumbnail and base
 $count = 0;
 $mediaAttribute = array (
 'thumbnail',
 'small_image',
 'image'
 );
 foreach ($imgArray as $image) :
 $imgUrl = $this->_save_image( $image ); 
 if ($count == 0) :
 $newproduct->addImageToMediaGallery( $imgUrl , $mediaAttribute, false, false ); 
 else :
 $newproduct->addImageToMediaGallery( $imgUrl , null, false, false );
 endif;
 $count++; 
 endforeach; 

And the _save_image method;

protected function _save_image($img) {
 $imageFilename = basename($img); 
 $image_type = substr(strrchr($imageFilename,"."),1); //find the image extension
 $filename = md5($img . strtotime('now')).'.'.$image_type; //give a new name, you can modify as per your requirement
 $filepath = Mage::getBaseDir('media') . DS . 'import'. DS . $filename; //path for temp storage folder: ./media/import/
 $newImgUrl = file_put_contents($filepath, file_get_contents(trim($img))); //store the image from external url to the temp storage folder
 return $filepath;
}

If you are using similar and it isn't working you might want to look at filepaths, permissions and also reindexing as potential issues that could fix your problem.

answered Jul 14, 2013 at 23:33
4
  • What is $mgArray ?? What will it contain ? Your answer is Incomplete. Please consider editing it to make it clear. Commented Sep 4, 2015 at 12:47
  • @PratikCJoshi - read the second sentence of the answer, it's an array of images. It's called $imgArray, so it will contain urls of the images you want to import into that product's gallery. The first image in the array is used as the thumbnail, small_image and image. Commented Sep 4, 2015 at 13:16
  • So you mean it will be $_FILES array ? Commented Sep 4, 2015 at 13:27
  • @PratikCJoshi No I mean something like $imgArray = array('http://ecx.images-amazon.com/images/I/214RgVjsvTL.jpg','http://ecx.images-amazon.com/images/I/515pMJlul8L.jpg'); Commented Sep 4, 2015 at 13:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.