首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Magento以编程方式删除产品图像

Magento以编程方式删除产品图像
EN

Stack Overflow用户
提问于 2011-04-19 06:10:29
回答 14查看 52.7K关注 0票数 22

这一定是一个如此简单的编程任务,以至于我在网上完全找不到任何关于它的信息。基本上,我是在尝试删除产品图片。我要删除产品媒体集中的所有图像。对于这样一个简单的任务,我可以在不费力通过一百万行代码的情况下做到这一点吗?

请注意,我已经尝试过了:

代码语言:javascript
复制
$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']);
        }
    }
}

这绝对是行不通的。我正在尝试在导入过程中删除图像,这样我就不会不断累积重复的图像。任何帮助都将不胜感激。

EN

回答 14

Stack Overflow用户

发布于 2011-04-20 00:26:59

好了,这就是我最终解决问题的方法。

代码语言:javascript
复制
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()那么简单,嗯?

票数 47
EN

Stack Overflow用户

发布于 2013-01-08 15:57:18

在Magento 1.7.0.2中,我使用以下代码从产品库中删除所有图像:

代码语言:javascript
复制
//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)’。

票数 9
EN

Stack Overflow用户

发布于 2011-07-05 00:26:54

要从产品库中删除所有图像:

代码语言:javascript
复制
$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();
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5709496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档