I have the following function for auto uploading images and setting the default one:
function importImages($sku, $image_url, $is_default){
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
$image_type = substr(strrchr($image_url,"."),1); //find the image extension
$filename = md5($image_url . $sku).'.'.$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/
file_put_contents($filepath, file_get_contents(trim($image_url))); //store the image from external url to the temp storage folder
if($is_default==true){
//$product->setMediaGallery(array('images'=>array (), 'values'=>array ()));
$mediaAttribute = array ( 'image',
'small_image',
'thumbnail'
);
}else{
$mediaAttribute = null;
}
$product->addImageToMediaGallery($filepath, $mediaAttribute, true, false);
$product->save();
}
The problem is that the default images isn't setted up as expected.
Best Answer
I have experienced that this would not store the images as default images in a multi side store. In order to accomplish this, I had to add the following code after
$product->save();
: