/**
* 导入某个商品图片,先清空再导入
* @param unknown $images 远程图片地址数组
* @param unknown $sku 商品sku.
*/
public function importProductImages($images,$sku) {
//remove All images
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
$product = Mage::getModel('catalog/product')->load($product->getId());
//deleting
$mediaGalleryAttribute = Mage::getModel('catalog/resource_eav_attribute')->loadByCode(4, 'media_gallery');
$gallery = $product->getMediaGalleryImages();
foreach ($gallery as $image)
$mediaGalleryAttribute->getBackend()->removeImage($product, $image->getFile());
$product->save();
//import product images
if (count($images)) {
//download image from remote server
$mediaAttribute = array (
'thumbnail',
'small_image',
'image'
);
$count_i = 0;
foreach ($images as $key=>$image) {
$file_name = basename($image);
$outPath = BP. DS .'media'.DS .'import'.DS.'images'.DS;
if (file_exists($outPath)) {
@unlink($outPath);
} else {
mkdir($outPath,0777);
}
if (substr(basename($this->_old_site . $image,".jpg"),-1) != 'm') {
++$count_i;
$this->save_image($this->_old_site . $image,$outPath . $file_name);
//save image to magento
if ($count_i == 1) {
$product->addImageToMediaGallery($outPath . $file_name, $mediaAttribute, false, false);
} else {
$product->addImageToMediaGallery($outPath . $file_name, null, false, false);
}
}
}
$product->save();
}
}