Magento产品图片上传在我的代码中不起作用


Magento Product Images upload not working in my code

我有下面的代码,应该使用csv文件中的产品sku更新Magento中的产品图像。

在我的csv文件中,有两列,第一列在产品SKU中,第二列在图像名称中。

 <?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 . '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);
    ?>

请让我知道我的代码出了什么问题。

请参考此示例,https://magento.stackexchange.com/questions/69944/magento-add-image-to-media-gallery-causing-duplicate-images-with-1-appended

foreach ($skuImageMap as $sku => $images) {
    $ourProduct = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
    $saveFlag = true;
    if ($ourProduct) {
        foreach ($images as $index => $imagePath) {
            if (file_exists($imagePath)) {
                try {
                    $ourProduct->addImageToMediaGallery($imagePath, null, false, false);
                    $imageCount++;
                } catch (Exception $e) {
                    $saveFlag = false;
                    echo "There was an issue saving image $imagePath to $sku :" . $e->getMessage() . "'n";
                }
            } else {
                $saveFlag = false;
                echo "The image $imagePath does not exist.'n";
            }
        }
        if($saveFlag) {
            try {
                $ourProduct->save();
                echo "The image $imagePath was added to $sku successfully : $imageCount 'n";
            } catch (Exception $e) {
                zend_debug::dump($e->getMessage());
            }
        }
    } else {
        echo "The product $sku does not exist 'n";
    }
}