Magento code add pictures to the product

0

To add pictures to the product, put them into the according folder.
With this code, I used to build a module have import product function and you can use anywhere it.

Before, you need a array include image type. Here is thumbnail, small_image and image. All images put into the according folder. I will use “media/import/”.

$product = Mage::getModel('catalog/product')->load($product_id);

$images = array(
	'image1.jpg' => 'thumbnail', 
	'image2.jpg' => 'small_image', 
	'image3.jpg' => '', 
	'image4.jpg' => '', 
	'image5.jpg' => 'image'
);

$dir = Mage::getBaseDir('media') . DS . 'import/';
foreach ($images as $image => $type) {
  $path = $dir . $image;
  if (file_exists($path)) {
    try {	
	  $product->addImageToMediaGallery($path, $type, false, false);
    } catch (Exception $e) {
      echo $e->getMessage();
    }
  } else {
    echo "Can not find image by path: `{$path}`<br/>";
  }
}

 

Next step: run this code and see result.

Magento add pictures to the product
Thanks for watching!

About Author

Là một developer rất ham chơi nhưng không ham hố, không ham chơi nhưng rất thích cầm đầu...

Comments are closed.

%d bloggers like this: