Magento programmatically add product image

后端 未结 2 1381
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 07:03

I have to create a simple Magento 1.6.x import agent that suppose to create/update products and their images. Could someone advise me how to add product image without having

2条回答
  •  伪装坚强ぢ
    2020-12-13 07:53

    I did this in Magento 1.6.1. Just put your image URL paths in the first array and you are good to go.

    Also look at Mage_Catalog_Model_Product to become familiar with addImageToMediaGallery() and other methods you'll undoubtedly need to be aware of in the future.

    // Add three image sizes to media gallery
    $mediaArray = array(
        'thumbnail'   => $putPathHere,
        'small_image' => $putPathHere,
        'image'       => $putPathHere,
    );
    
    // Remove unset images, add image to gallery if exists
    $importDir = Mage::getBaseDir('media') . DS . 'import/';
    
    foreach($mediaArray as $imageType => $fileName) {
        $filePath = $importDir.$fileName;
        if ( file_exists($filePath) ) {
            try {
                $product->addImageToMediaGallery($filePath, $imageType, false);
            } catch (Exception $e) {
                echo $e->getMessage();
            }
        } else {
            echo "Product does not have an image or the path is incorrect. Path was: {$filePath}
    "; } }

提交回复
热议问题