Magento programmatically add product image

后端 未结 2 1379
被撕碎了的回忆
被撕碎了的回忆 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:45
    set_time_limit(0);
    
    ini_set('memory_limit', '4095M');
    
    error_reporting(E_ALL);
    
    ini_set('display_errors', 1);
    
    require_once '../app/Mage.php';
    
    umask(0);
    
    Mage::setIsDeveloperMode(true);
    
    $storeID = Mage_Core_Model_App::ADMIN_STORE_ID;
    
    Mage::app()->setCurrentStore($storeID);
    
    
    
    $destination = Mage::getBaseDir() . '/import/images/' . $image;
    
    $product->addImageToMediaGallery($destination, array('image', 'thumbnail', 'small_image'), false, false);
    
    }
    

    This will set the base image.

    0 讨论(0)
  • 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}<br/>";
        }
    }
    
    0 讨论(0)
提交回复
热议问题