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

时间:2011-12-10 13:49:44

标签: 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'));

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

由于

的Lukas

2 个答案:

答案 0 :(得分:38)

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

答案 1 :(得分:0)

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

}

这将设置基本图像。

相关问题