我正在以编程方式创建产品和图像。唯一不起作用的是"从图库中排除"功能。
有关详细信息,请参阅 http://snag.gy/QGhPg.jpg。
我将如何在代码中设置它?我当前的代码如下所示:
$product->addImageToMediaGallery($myImage, array('image','thumbnail','small_image'), false, false);
我认为这面旗帜被称为disabled
,但不确定。
谢谢!
例如,禁用产品的所有图像
$product = Mage::getModel('catalog/product')->load(12345);
$images = $product->getMediaGalleryImages();
$attributes = $product->getTypeInstance(true)->getSetAttributes($product);
$mediaGalleryAttribute = $attributes['media_gallery'];
foreach ($images as $image) {
$mediaGalleryAttribute->getBackend()->updateImage($product, $image['file'], array('exclude' => true));
}
$product->save();
尝试使用:
$product->addImageToMediaGallery($myImage, array('image','thumbnail','small_image'), false, true);
最后一个参数应设置为 true
以从库中排除图像。
请参阅:app/code/core/Mage/Catalog/Model/Product.php
。
试试下面的代码。首先保存产品并插入图像
$productid=10; // Product id of that product which image you want to update
//for remove existing Images
$loadpro=Mage::getModel('catalog/product')->load($productid);
$mediaApi = Mage::getModel("catalog/product_attribute_media_api");
$mediaApiItems = $mediaApi->items($loadpro->getId());
foreach($mediaApiItems as $item) {
$datatemp=$mediaApi->remove($loadpro->getId(), $item['file']);
}
$loadpro->save(); //before adding need to save product
//for add new images
$loadpro=Mage::getModel('catalog/product')->load($loadpro->getId());
$loadpro->addImageToMediaGallery($loadpro, array ('image','small_image','thumbnail'), true, false);
$mediaArray = array(
'thumbnail' => $oscProduct['products_image'],
'small_image' => $oscProduct['products_mediumimage'],
'image' => $oscProduct['products_largeimage'],
);
// Remove unset images, add image to gallery if exists
$importDir = Mage::getBaseDir('media') . DS . 'import/';
foreach ( $mediaArray as $imageType => $fileName ) {
$filePath = $importDir . $fileName;
if ( file_exists($filePath) ) {
try {
$product->addImageToMediaGallery($filePath, $imageType, false);
} catch (Exception $e) {
echo $e->getMessage();
}
} else {
echo "Product does not have an image or the path is incorrect. Path was: {$filePath}<br/>";
}
}
注意:$imageType不像您的对应物那样是一个数组。
编辑:您只需要一个图像,并将其设置为每种类型。使用 $product->save() 保存产品后,请尝试类似以下内容的操作,假设您已经设置了图像类型:
$product->setThumbnail($product->getImage());
$product->setSmallImage($product->getImage());
$product->save();