magento remove or add image to product programmly

1926 단어 remove
//remove product images:
public function remove_image_files($_product_id) {
		if ($_product_id) {
			$simple_product = Mage::getModel('catalog/product')->load($_product_id);
			//clean all product images;
			$mediaApi = Mage::getModel("catalog/product_attribute_media_api");
			try {
				$items = $mediaApi->items($simple_product->getId());
				foreach($items as $item) {
					echo ($mediaApi->remove($simple_product->getId(), $item['file']));
				}
			} catch (Exception $exception){
				var_dump($exception);
				die('Exception Thrown');
			}
		}
	}

 //add image to product
	public function import_image($_product_id,$config_sku,$config_label) {
		if ($_product_id) {
			$simple_product = Mage::getModel('catalog/product')->load($_product_id);
			//import product images
			$importDir = $this->_file_path .$config_sku.DS . $config_label . DS;
			$i = 0;
			foreach ($this->_image_arr as $imageType=>$fileName) {
				var_dump($imageType);
				$filePath = $importDir.$fileName;
				var_dump($filePath);
				++$i;
				if (file_exists($filePath)) {
					try {
						$simple_product->addImageToMediaGallery($filePath, array($imageType), false, false);
						var_dump('save image:' . $filePath .' successed!');
					} catch (Exception $e) {
						echo $e->getMessage();
					}
				} else {
					var_dump('Not Found Image for :' . $config_sku.'-'. $config_label . ' ' . $imageType);
				}
			}
			try {
				$simple_product->save();
			}catch (Exception $e) {
				echo $e->getMessage();
			}
			
		}
	}

좋은 웹페이지 즐겨찾기