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

Ber*_*ery 19 php magento

我必须创建一个简单的Magento 1.6.x导入代理,假设创建/更新产品及其图像.有人可以告诉我如何在不使用magento API的情况下添加产品图片吗?

api表现非常糟糕,我开始有点沮丧.. :-(

我已经找到了一些关于这个问题的其他问题,但是没有一个问题涉及向产品添加图像.

这就是我的意思:

$product->setIsMassupdate(true)
    ->setExcludeUrlRewrite(true)
    ->setManufacturer($this->addManufacturers(utf8_encode($record[4])))
    ->setSku($record[3])
    ->setAttributeSetId($this->attribute_set)# 9 is for default
    ->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE)
    ->setName(utf8_encode($record[5]))
    ->setCategoryIds($this->getCategories(array($record[0], $record[1], $record[2]))) # some cat id's,
    ->setWebsiteIDs(array(1)) # Website id, 1 is default
    ->setDescription(utf8_encode($record[6]))
    ->setShortDescription($this->shortText(utf8_encode($record[6]), 150))
    ->setPrice($price) # Set some price
    ->setSpecialPrice($special_price)
    ->setWeight($record[12])
    ->setStatus( Mage_Catalog_Model_Product_Status::STATUS_ENABLED )
    ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
    ->setTaxClassId(2)     // default tax class
    ->setPixmaniaimg($record[10])
    ->setStockData(array('is_in_stock' => $inStock, 'qty' => $qty))
    ->setCreatedAt(strtotime('now'));
Run Code Online (Sandbox Code Playgroud)

没有API可以帮助我直接添加图片吗?

谢谢

卢卡斯

Zac*_*ler 39

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

另请查看Mage_Catalog_Model_Product以熟悉addImageToMediaGallery()和其他您将来需要注意的方法.

// 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/>";
    }
}
Run Code Online (Sandbox Code Playgroud)