Magento以编程方式添加产品图片 [英] Magento programmatically add product image

查看:86
本文介绍了Magento以编程方式添加产品图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 to use the magento API?

事实证明,api的性能非常差,我开始感到有点沮丧.:-(

The api performance turned out to be very poor and I am starting to be a little frustrated.. :-(

我发现了有关此问题的其他一些问题,但它们都与将图像添加到产品无关.

I have found some other questions regarding this problem, but none of them concerns with adding images to the product.

这是我附带的:

$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'));

有人可以在没有API的情况下帮助我直接添加图像吗?

Can someone help me with adding images directly without the API?

谢谢

卢卡斯

推荐答案

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

这篇关于Magento以编程方式添加产品图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆