0

I have below code which should update product images in Magento using product sku from csv file.
In my csv file, there are two columns, First column in Product SKU and second with Image Name.

<?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    set_time_limit(0);

    require_once 'app/Mage.php';
    Mage::app();
    //Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);

    $connection = Mage::getSingleton('core/resource')->getConnection('core_write'); 
    define('CSV_PATH','csvfiles/');

    $csv_fileoutput = CSV_PATH . "output_imagesupload.csv";
    $csvfileoutput = fopen($csv_fileoutput, 'a');

    $importDir = Mage::getBaseDir('media') . DS . 'catalog/product/incoming/';
    $file_handle = fopen("csvfiles/images_insert.csv", "r");
    while (!feof($file_handle) ) {
        $line_of_text = fgetcsv($file_handle, 1024);
        $productSKU = $line_of_text[0];
        $productid = Mage::getModel('catalog/product')->getIdBySku($productSKU);
        if ($productid) 
        {
            $Products = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU);
            $fileName = $line_of_text[1];
            $filePath = $importDir.$fileName;

            if(file_exists($filePath)) {

                $Products->addImageToMediaGallery($filePath, array('image', 'small_image', 'thumbnail'), true, false);
                $Products->save();


            /*  $Products->addImageToMediaGallery($filePath,'image',true,false);
                $Products->save();
*/
    /*  
                $setbaseimage=Mage::getModel('catalog/product')->load($objProduct);
                $setbaseimage->addImageToMediaGallery($filePath,array('image','small_image','thumbnail'),true,false);
                Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
                $setbaseimage->save();
    */
            } else{
                echo $productSKU . " Not done \n";

            }
                $my_product = Mage::getModel('catalog/product')->load($productid); 
                $url = $my_product->getProductUrl();

                fputcsv($csvfileoutput, array($productSKU,$url));   
        }
        else {echo $productSKU." - Product is not available\n"; }

    }
    fclose($file_handle);

    ?>

Below is my csv file data -

WPL8573228  8573239-WPL-N-8.jpg
WPL8573229  8573240-WPL-N-8.jpg
WPL8573230  8573241-WPL-N-8.jpg
WPL8573239  8573242-WPL-N-8.jpg

Please let me know what is wrong with my code.

  • What is the error you are getting? – Charlie Apr 29 '16 at 5:36
  • No error. But Images are not assigning to that product. I have uploaded images on /media/catalog/product/incoming directory and in csv file image name is added. – Rahul Apr 29 '16 at 5:39
  • Did you checked $filePath showing right path of the file? right now I am not able to run your code so – Charlie Apr 29 '16 at 5:54
  • Yes that was error .. But after echo and changing path it still not uploading images – Rahul Apr 29 '16 at 6:02
  • I am not sure, but you are facing problem in file path, image name etc.. debug them step by step by printing filepath and file name. – Charlie Apr 29 '16 at 6:11
0

Follow bellow steps:

Step : 1 Upload your csv file with file name products.csv in var folder and file data is

----------------------------------
Product SKU Image Name
----------------------------------
WPL8573228  8573239-WPL-N-8.jpg
----------------------------------
WPL8573229  8573240-WPL-N-8.jpg
----------------------------------
WPL8573230  8573241-WPL-N-8.jpg
----------------------------------
WPL8573239  8573242-WPL-N-8.jpg

Step : 2 Add your images in folder media\catalog\product\incoming.

Step : 3 Create sample.php file at magento root folder and add bellow code in file

<?php
require_once('app/Mage.php');
Mage::app('default');

class ABDUL
{
    public function index()    
    {
        $filePath = 'var/products.csv';
        $csvObject = new Varien_File_Csv();
        $importDir = Mage::getBaseDir('media') . DS . 'catalog/product/incoming/';
        try {
            $productsData = $csvObject->getData($filePath);
            $count = 0;
            foreach($productsData as $productData) {
                if($count > 0) {
                    $sku        = $productData[0];
                    $imageName  = $productData[1];
                    $product = Mage::helper('catalog/product')->getProduct($sku, null, 'sku');  
                    if($product->getId()) {
                        $imagePath = $importDir.$imageName;
                        if(file_exists($imagePath)) {
                            $product->addImageToMediaGallery($imagePath, array ('image','small_image','thumbnail'), true, false);
                            $product->save();
                            echo "{$sku} - Product image has been added.</br>";
                        }
                    }else {
                       echo "{$sku} - Product is not available</br>";
                    }

                }
                $count++;
            }
        } catch (Exception $e) {
            $e->getMessage();
        }
    }
}
$obj = new ABDUL();
$obj->index();
?>

Step : 4 Run sample.php file in your browser and after check those products.

  • No its not working. It hangs my system and terminal. – Rahul Apr 29 '16 at 10:39
  • It would be better if you provide solution on my code.. – Rahul Apr 29 '16 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.