简体   繁体   English

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

[英]Magento Product Images upload not working in my code

I have below code which should update product images in Magento using product sku from csv file.我有以下代码,它应该使用 csv 文件中的产品 sku 更新 Magento 中的产品图像。

In my csv file, there are two columns, First column in Product SKU and second with Image Name.在我的 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);

    ?>

Please let me know what is wrong with my code.请让我知道我的代码有什么问题。

Please refer this for example, https://magento.stackexchange.com/questions/69944/magento-add-image-to-media-gallery-causing-duplicate-images-with-1-appended例如,请参考这个, 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";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM