Created
February 8, 2017 15:18
-
-
Save meminuygur/7c6452bdc6cad18e70c26fc6a76e4d8f to your computer and use it in GitHub Desktop.
Magento Add Product Images From Remote Url Programmatically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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