下面的代码允许您导入图像并设置位置。它将根据数组中图像的顺序设置位置,因此如果这不是必需的,则您需要更改该位置,但希望这至少能让您了解如何完成。
$sku = $product->getSku();
$media = Mage::getModel('catalog/product_attribute_media_api');
$position = 1;
foreach($mediaArray as $fileName) {
if (file_exists($fileName)) { // assuming $fileName is full path not just the file name
$pathInfo = pathinfo($fileName);
switch($pathInfo['extension']){
case 'png':
$mimeType = 'image/png';
break;
case 'jpg':
$mimeType = 'image/jpeg';
break;
case 'gif':
$mimeType = 'image/gif';
break;
}
$types = ($position == 1) ? array('image', 'small_image', 'thumbnail') : array();
$newImage = array(
'file' => array(
'content' => base64_encode($fileName),
'mime' => $mimeType,
'name' => basename($fileName),
),
'label' => 'whatever', // change this.
'position' => $position,
'types' => $types,
'exclude' => 0,
);
$media->create($sku, $newImage);
// OR (if you would rather use the product entity ID):
// $media->create($productId, $newImage, null, 'id');
$position++;
} else {
// image not found
}
}