Skip to content

Instantly share code, notes, and snippets.

@meminuygur
Created February 8, 2017 15:18
Show Gist options
  • Save meminuygur/7c6452bdc6cad18e70c26fc6a76e4d8f to your computer and use it in GitHub Desktop.
Save meminuygur/7c6452bdc6cad18e70c26fc6a76e4d8f to your computer and use it in GitHub Desktop.
Magento Add Product Images From Remote Url Programmatically
<?php
$product_id = 12; // Product ID
$product = Mage::getModel('catalog/product')->load($product_id);
$remoteImages = [
'http://www.example.com/dummyImage.jpg',
'http://www.example.com/dummyImage1.jpg',
'http://www.example.com/dummyImage2.jpg'
];
$product->setMediaGallery(array('images' => array(), 'values' => array())); //media gallery initialization
$i = 0;
foreach( $remoteImages as $img){
$i++;
// IF YOU WANT RENAME IMAGE
$image_url = $product->getSku().'_'.$i.'.jpg';
copy($img, $image_url);
if( copy($img->fotograf, $image_url) ) {
if( $i == 1) {
$product->addImageToMediaGallery($image_url, array('image', 'thumbnail', 'small_image'), false, false);
} else{
$product->addImageToMediaGallery($image_url, null, false, false);
}
}
}
try{
$product->save();
echo $product->getSku()." ==> OK\r\n";
} catch(Exception $e){
print_r($e->getMessage());
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment