Import Product Images

127 篇文章 0 订阅
	/**
	 * 导入某个商品图片,先清空再导入
	 * @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();
		}
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值