2011-12-10 94 views
19

我必须创建一个简单的Magento 1.6.x导入代理,以便创建/更新产品及其图像。有人能告诉我如何添加产品图像,而不必使用magento API?Magento以编程方式添加产品图像

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

+0

哪个版本的Magento? – benmarks

+0

Magento 1.6 - 对不起,在我原来的评论中忽略.. – Bery

+0

对于Magento 2:http://magento.stackexchange.com/questions/140612/magento-2-save-all-product-data-outside-magento-with -images –

回答

38

我在Magento 1.6.1中做了这个。把你的图片网址路径放在第一个数组中,你就可以走了。

另请参阅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/>"; 
    } 
} 
+0

有没有办法用magento创建图像?我只有一个尺寸的图像。 – Bery

+0

首先,你需要单独的图像吗?如果需要,Magento可让您将单个图像设置为缩略图,small_image和图像大小。 –

+0

好的。我需要使用一个或多个图像(每个产品最多为hree图像),然后使用 - 第一个作为基础图像,然后使用small_image和缩略图。图像总是相同的(例如,image1.jpg是基本的,small_image和缩略图)。 – Bery

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

} 

这将设置在基本图像。

相关问题