Magento – Updating product images programmatically

galleryimagemagento-1.7product

I have created one custom module where user(customer) can upload and edit his own product through frontend( my account ) dashboard. In this I have used code to edit the image and set it to thumbnail, base, and image for uploaded product. But it was not updating the Image. So Later on I tried to change through admin dashboard. Even after setting up thumbnail, small_image and and image but now it is not upating the product image for any product through admin dashboard(backend).

I have used the code

if (!$product->hasSmallImage()) $product->setSmallImage($product->getImage());
if (!$product->hasThumbnail()) $product->setThumbnail(Mage::helper('catalog/image')->init($product, 'thumbnail'));

I dont know where should check for the error as it is not showing error on admin dashboard as well. I have checked the database for product_entity table and for gallery and it is showing the image.

Best Answer

Have you called $product->save() method after below code?

if (!$product->hasSmallImage())
   $product->setSmallImage($product->getImage());
if (!$product->hasThumbnail()) 
   $product->setThumbnail(Mage::helper('catalog/image')->init($product, array('thumbnail','small_image','image'))); 

Update:: set the Small Image, Thumbnail and image.