且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Magento 以编程方式添加产品图片

更新时间:2023-08-26 15:31:34

我在 Magento 1.6.1 中做到了这一点.只需将您的图片 URL 路径放在第一个数组中即可.

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

另请参阅 Mage_Catalog_Model_Product 以熟悉 addImageToMediaGallery() 和您无疑需要熟悉的其他方法将来知道.

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/>";
    }
}