Home

Thursday, June 22, 2017

How to add images for products in magento pragmatically ?

Use can upload Base-Image, Small-Image, Thumbnail-Image, and Image-Gallery for the product using CSV file in Magento programmatically.
=====================================================================
<?php
require_once '../app/Mage.php';
set_time_limit(0);
ini_set('memory_limit','1024M');
Mage::app();
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);

// Images with SKU
$importDir = Mage::getBaseDir('media') . DS . 'import/diamond/shape/big/';
$file_handle = fopen("images5.csv", "r");
$c = 0;


while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 10000);

if($line_of_text[0] !="")
{
$productSKU = $line_of_text[0];
$ourProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU);
$lastproductSKU = $productSKU;

$basefileName = $line_of_text[1];
$smallfileName = $line_of_text[2];
$thumbfileName = $line_of_text[3];

$basefilePath = $importDir.$basefileName;
$smallfilePath = $importDir.$smallfileName;
$thumbfilePath = $importDir.$thumbfileName;

// Base Image
if (file_exists($basefilePath)) {
$ourProduct->addImageToMediaGallery($basefilePath, array('image'), false, false);
}

// Small Image
if (file_exists($smallfilePath)) {
$ourProduct->addImageToMediaGallery($smallfilePath, array('small_image'), false, false);
}

// thumbnail Image
if (file_exists($thumbfilePath)) {
$ourProduct->addImageToMediaGallery($thumbfilePath, array('thumbnail'), false, false);
}

$extrafileName = $line_of_text[4];
$extrafilePath = $importDir.$extrafileName;
if (file_exists($extrafilePath)) {
$ourProduct->addImageToMediaGallery($extrafilePath, null, false, false);
}
$ourProduct->save();
}else
{
$ourProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$lastproductSKU);
$extrafileName = $line_of_text[4];
$extrafilePath = $importDir.$extrafileName;
if (file_exists($extrafilePath)) {
$ourProduct->addImageToMediaGallery($extrafilePath, null, false, false);
$ourProduct->save();
}
}
$c++;
}
fclose($file_handle);
?>

Please check image for CSV format.


No comments:

Post a Comment

rathoddhirendra.blogspot.com-Google pagerank and Worth