How Can I Add Images To Magento Product Programmatically

How can I attach the images to the Magento product programmatically

You will need to upload the Magento product images to the server first, then attach the image path to the specific Magento product programmatically

$importDir = 'productimage/1/';
$productsData = array('image1.jpg','image2.jpg');

foreach($productsData as $fileName){
    $productSKU = '1';
    $ourProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU);
    $filePath = $importDir.$fileName;
    if (file_exists($filePath)) {
        $ourProduct->addImageToMediaGallery($filePath, array('image', 'small_image', 'thumbnail'), false, false);
        $ourProduct->save();
    }
}
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments