假設您已加載產品,並且您已準備好進行更改並保存它們。用此代碼:
if (file_exists($imagePath)) {//New image file
//Load your media table
$mediaApi = Mage::getModel("catalog/product_attribute_media_api");
try {
//Now you have all the images available for your product
//if you previously have assign anything
$items = $mediaApi->items($product->getId());
//loop them
foreach ($items as $item) {
//With that line you can remove them if you want
echo ($mediaApi->remove($product->getId(), $item['file']));
}
} catch (Exception $exception) {
var_dump($exception);
die('Exception Thrown');
}
$cache = Mage::getSingleton('core/cache');
$cache->flush();
//You need that line
$product->setMediaGallery(array('images' => array(), 'values' => array()));
//That line assigns your new image.As base,thumbail and image.
//Use it in the loop if you want to reassign an existing image.
$product->addImageToMediaGallery($imagePath, array('thumbnail', 'small_image', 'image'), false, false);
我希望能幫到你 乾杯!