Magento – update product thumbnail ,small image and base image programatically

Magento – update product thumbnail ,small image and base image programatically

In this post we will show you how to update image( thumbnail ,small and base) programatically in Magento. by using this code we can update product image and also run this out of Magento.

for run this code we need to add image in to pass image path in $to_path and you have to update base or small or thumbnail image only then pass it in $set_media_attribute. In this method we can update product image using sku or by using product id both way.

/* 
* if run file exteranl php
* require_once("app/Mage.php");
* umask(0);
* Mage::app();
*/

// path of image to uplode 
$to_path = "media/import/my-image.jpg";

/*  
* if you have to update image using 
* sku t use this 
* pass your sku
*/
$product_sku = $set_sku;	
$product_id = Mage::getModel("catalog/product")->getIdBySku( $product_sku ); 

// or pass your product id
// $product_id = "98526";

try { 
	// pass product id
	$set_product = Mage::getModel('catalog/product')->load($product_id);

	// set media attribute
	$set_media_attribute = array (
				'image',
				'thumbnail',
				'small_image'                  
			);

	$set_product->addImageToMediaGallery($to_path, $set_media_attribute, true, false);
	// save iamge
	$set_product->save(); 
}
catch(Exception $exception) {  
	// print exception
	var_dump($exception);
}

Leave a Comment

Your email address will not be published. Required fields are marked *

4  +  5  =  

We accept paid advance and paid guest Posting on our Site : Contact US