这一定是一个如此简单的编程任务,以至于我在网上完全找不到任何关于它的信息。基本上,我是在尝试删除产品图片。我要删除产品媒体集中的所有图像。对于这样一个简单的任务,我可以在不费力通过一百万行代码的情况下做到这一点吗?
请注意,我已经尝试过了:
$attributes = $product->getTypeInstance()->getSetAttributes();
if (isset($attributes['media_gallery'])) {
$gallery = $attributes['media_gallery'];
$galleryData = $product->getMediaGallery();//this returns NULL
foreach($galleryData['images'] as $image){
if ($gallery->getBackend()->getImage($product, $image['file'])) {
$gallery->getBackend()->removeImage($product, $image['file']);
}
}
}这绝对是行不通的。我正在尝试在导入过程中删除图像,这样我就不会不断累积重复的图像。任何帮助都将不胜感激。
发布于 2011-04-20 00:26:59
好了,这就是我最终解决问题的方法。
if ($product->getId()){
$mediaApi = Mage::getModel("catalog/product_attribute_media_api");
$items = $mediaApi->items($product->getId());
foreach($items as $item)
$mediaApi->remove($product->getId(), $item['file']);
}这就是最终让我头脑清醒的链接:http://www.magentocommerce.com/wiki/doc/webservices-api/api/catalog_product_attribute_media
太糟糕了,它不像$product->getImages()那么简单,嗯?
发布于 2013-01-08 15:57:18
在Magento 1.7.0.2中,我使用以下代码从产品库中删除所有图像:
//deleting
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
$mediaApi = Mage::getModel("catalog/product_attribute_media_api");
$items = $mediaApi->items($product->getId());
$attributes = $product->getTypeInstance()->getSetAttributes();
$gallery = $attributes['media_gallery'];
foreach($items as $item){
if ($gallery->getBackend()->getImage($product, $item['file'])) {
$gallery->getBackend()->removeImage($product, $item['file']);
}
}
$product->save();使用Davids Tay answer中的代码,我得到了错误:致命错误:未捕获异常‘Mage_Eav_Model_Entity_Attribute_Exception’,并显示消息‘SQLSTATE23000:完整性约束违规: 1452无法添加或更新子行:外键约束失败(base_xxx.catalog_product_entity_media_gallery_value,CONSTRAINT FK_CAT_PRD_ENTT_MDA_GLR_VAL_VAL_ID_CAT_PRD_ENTT_MDA_GLR_VAL_ID外键(value_id)引用`catalog_prod)’。
发布于 2011-07-05 00:26:54
要从产品库中删除所有图像:
$product = Mage::getModel('catalog/product')->load($id);
$mediaGalleryAttribute = Mage::getModel('catalog/resource_eav_attribute')->loadByCode($entityTypeId, 'media_gallery');
$gallery = $product->getMediaGalleryImages();
foreach ($gallery as $image)
$mediaGalleryAttribute->getBackend()->removeImage($product, $image->getFile());
$product->save();https://stackoverflow.com/questions/5709496
复制相似问题