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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8456954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:46:16  来源:igfitidea点击:

Magento programmatically add product image

phpmagento

提问by Bery

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?

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

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

结果证明 api 性能很差,我开始有点沮丧.. :-(

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

我发现了一些关于此问题的其他问题,但没有一个与向产品添加图像有关。

This is what I came with:

这是我带来的:

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

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

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

Thanks

谢谢

Lukas

卢卡斯

回答by Zachary Schuessler

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

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

Also look at Mage_Catalog_Model_Productto become familiar with addImageToMediaGallery() and other methods you'll undoubtedly need to be aware of in the future.

另请查看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/>";
    }
}

回答by Amit Singh

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.

这将设置基本图像。