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

发布于 2024-11-02 01:01:36 字数 660 浏览 1 评论 0原文

这肯定是一个非常简单的编程任务,我绝对无法在网上找到任何有关它的信息。基本上,我正在尝试删除产品图像。我想删除产品媒体库中的所有图像。我可以在不为如此简单的任务编写一百万行代码的情况下完成此操作吗?

请注意,我已经尝试过:

$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']);
        }
    }
}

这绝对行不通。我试图在导入过程中删除图像,这样我就不会不断产生重复项。任何帮助将不胜感激。

This must be a such a simple programming task that I absolutely cannot find any information about it on the net. Basically, I'm trying to DELETE product images. I want to delete all images from a product's media gallery. Can I do this without wading through a million lines of code for such a simple task?

Please note that I've already tried this:

$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']);
        }
    }
}

This absolutely does not work. I'm trying to delete images during an import so that I do not keep accruing duplicates. Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

风轻花落早 2024-11-09 01:01:37

在 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 答案中的代码,我收到错误:
致命错误:未捕获异常“Mage_Eav_Model_Entity_Attribute_Exception”,消息为“SQLSTATE[23000]:违反完整性约束:1452 无法添加或更新子行:外键约束失败(base_xxxcatalog_product_entity_media_gallery_value,约束 FK_CAT_PRD_ENTT_MDA_GLR_VAL_VAL_ID_CAT_PRD_ENTT_MDA_GLR_VAL_ID 外键(value_id)引用 `catalog_prod)'

In Magento 1.7.0.2 I use this code for remove all images from a product gallery:

//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();

With code from Davids Tay answer, I got the error:
Fatal error: Uncaught exception ‘Mage_Eav_Model_Entity_Attribute_Exception’ with message ‘SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (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 FOREIGN KEY (value_id) REFERENCES `catalog_prod)’

烟雨凡馨 2024-11-09 01:01:37

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

$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();

To remove all images from a product gallery:

$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();
旧情别恋 2024-11-09 01:01:37

在删除图像的过程中我发现了一件有趣的事情。当您尝试此代码时:

$galleryData = $product->getMediaGallery(); //this returns NULL

媒体库对象取决于您的产品的创建方式,如果:

$product = Mage::getModel('catalog/product')->loadByAttr('sku', $sku);

则媒体库将为空,如果:

$product = Mage::getModel('catalog/product')->load($id);

则媒体库是对象,您可以使用此对象从数据库中删除图像。
要从文件系统中删除图像,您必须添加这样的代码:

@unlink(Mage::getBaseDir('media') . '\catalog\product\' . $image['file']);

但如果您想更改图像并将其命名为以前的图像(image1.png 替换为 image1.png),在这种情况下,您将遇到浏览器缓存问题。

您也可以尝试加载产品的媒体库:

$product->getResource()->getAttribute('media_gallery')->getBackend()->afterLoad($product);

During deleting of images I found one interesting thing. When you try this code:

$galleryData = $product->getMediaGallery(); //this returns NULL

Media gallery object depends on how your product was created, if:

$product = Mage::getModel('catalog/product')->loadByAttr('sku', $sku);

then media gallery will be empty, if:

$product = Mage::getModel('catalog/product')->load($id);

then media gallery is object and you can use this object to delete images from db.
To delete images from file system you have to add such code:

@unlink(Mage::getBaseDir('media') . '\catalog\product\' . $image['file']);

but if you want to change image and name it like previous image (image1.png replace with image1.png) in this case you will have browser caching issue.

Also you can try to load media gallery for product:

$product->getResource()->getAttribute('media_gallery')->getBackend()->afterLoad($product);
ㄟ。诗瑗 2024-11-09 01:01:37

这是我最终决定用于此任务的代码。

protected function _removeMediaGalleryImages(Mage_Catalog_Model_Product $product)
{
    $mediaGalleryData = $product->getMediaGallery();
    if (!isset($mediaGalleryData['images']) || !is_array($mediaGalleryData['images'])) {
        return;
    }

    $toDelete = array();
    foreach ($mediaGalleryData['images'] as $image) {
        $toDelete[] = $image['value_id'];
    }
    Mage::getResourceModel('catalog/product_attribute_backend_media')->deleteGallery($toDelete);
}

This is the code I finally decided to use for this task.

protected function _removeMediaGalleryImages(Mage_Catalog_Model_Product $product)
{
    $mediaGalleryData = $product->getMediaGallery();
    if (!isset($mediaGalleryData['images']) || !is_array($mediaGalleryData['images'])) {
        return;
    }

    $toDelete = array();
    foreach ($mediaGalleryData['images'] as $image) {
        $toDelete[] = $image['value_id'];
    }
    Mage::getResourceModel('catalog/product_attribute_backend_media')->deleteGallery($toDelete);
}
羁〃客ぐ 2024-11-09 01:01:37

希望这个答案不会不受欢迎,因为从技术上讲,它不是通过 Magento 编程实现的解决方案,正如您所要求的,但我已经成功地自己清除了所有画廊图像以达到相同的目的,只需截断 Magento 1.4.2.0 中的相关表(我相信它是1.5 中的表结构也相同)。

TRUNCATE TABLE `catalog_product_entity_media_gallery`
TRUNCATE TABLE `catalog_product_entity_media_gallery_value`

然后删除 /media/catalog/product 目录中的所有图像文件。

我确实自己寻找一种以编程方式执行此操作的方法,但发现这种方法效率更高,并且没有经历任何负面副作用。

Hopefully this answer isn't unwelcome, as it's technically not a solution achieved via Magento programming as you ask, but I have successfully purged all gallery images myself for the same purpose simply by truncating the relevant tables in Magento 1.4.2.0 (I believe it's the same table structure in 1.5 as well).

TRUNCATE TABLE `catalog_product_entity_media_gallery`
TRUNCATE TABLE `catalog_product_entity_media_gallery_value`

Then follow up by removing all the image files within the /media/catalog/product directory.

I did look for a way to do this programmatically myself, but found this much more efficient and have experienced no negative side effects.

遮了一弯 2024-11-09 01:01:37
     Just use this code to create .php file and place it to root folder of magento.

    <?php 
    require_once 'app/Mage.php';

    Mage::app();
    Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);

    $products = Mage::getModel('catalog/product')->getCollection();
      //->addAttributeToFilter('entity_id', array('gt' => 14000));

    $mediaApi = Mage::getModel("catalog/product_attribute_media_api");

    foreach($products as $product) {
        $prodID = $product->getId();
        $_product = Mage::getModel('catalog/product')->load($prodID);
        $items = $mediaApi->items($_product->getId());
        foreach($items as $item) {
            $mediaApi->remove($_product->getId(), $item['file']);
        }
    }
?>
     Just use this code to create .php file and place it to root folder of magento.

    <?php 
    require_once 'app/Mage.php';

    Mage::app();
    Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);

    $products = Mage::getModel('catalog/product')->getCollection();
      //->addAttributeToFilter('entity_id', array('gt' => 14000));

    $mediaApi = Mage::getModel("catalog/product_attribute_media_api");

    foreach($products as $product) {
        $prodID = $product->getId();
        $_product = Mage::getModel('catalog/product')->load($prodID);
        $items = $mediaApi->items($_product->getId());
        foreach($items as $item) {
            $mediaApi->remove($_product->getId(), $item['file']);
        }
    }
?>
攀登最高峰 2024-11-09 01:01:37

这又怎么样

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();

What About this

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();
月朦胧 2024-11-09 01:01:37

删除图像的最快方法,然后按照以下步骤操作:
表中的所有记录

  1. 删除catalog_product_entity_media_gallery
  2. catalog_product_entity_media_gallery_value'

,因为 magento 将所有产品图像数据保存在这些表中。

然后从管理员的索引管理中进行索引,将图像设置为黑色。

然后从目录中删除图像,然后转到位于 media/catalog/product 的 magento 目录,并从此文件夹中删除所有文件。

另一个过程:

Andy Simpson,您需要一个脚本,用于从系统中删除所有产品,从而从数据库和文件系统中删除

第1步:magento系统的根目录处创建一个php,其中在第一个代码处包含Mage.php

require_once "YOURMAGENTODIR/app/Mage.php";
umask(0);

Step2: 设置当前商店为 admin 并设置开发者模式

Mage::app('admin');
Mage::setIsDeveloperMode(true);

Step3: 获取Product Collection 并为获取创建一个循环一个接一个的产品

$productCollection=Mage::getResourceModel('catalog/product_collection');

第四步:逐一获取产品图像并使用以下代码删除图像:

$remove=Mage::getModel('catalog/product_attribute_media_api')->remove($product->getId(),$eachImge['file']);

完整代码:

<?php
require_once "YOURMAGENTODIR/app/Mage.php";
umask(0);
Mage::app('admin');
Mage::setIsDeveloperMode(true);

$productCollection=Mage::getResourceModel('catalog/product_collection');
foreach($productCollection as $product){
echo $product->getId();
echo "<br/>";
         $MediaDir=Mage::getConfig()->getOptions()->getMediaDir();
        echo $MediaCatalogDir=$MediaDir .DS . 'catalog' . DS . 'product';
echo "<br/>";

$MediaGallery=Mage::getModel('catalog/product_attribute_media_api')->items($product->getId());
echo "<pre>";
print_r($MediaGallery);
echo "</pre>";

    foreach($MediaGallery as $eachImge){
        $MediaDir=Mage::getConfig()->getOptions()->getMediaDir();
        $MediaCatalogDir=$MediaDir .DS . 'catalog' . DS . 'product';
        $DirImagePath=str_replace("/",DS,$eachImge['file']);
        $DirImagePath=$DirImagePath;
        // remove file from Dir

        $io     = new Varien_Io_File();
         $io->rm($MediaCatalogDir.$DirImagePath);

        $remove=Mage::getModel('catalog/product_attribute_media_api')->remove($product->getId(),$eachImge['file']);
    }


}

Fastest way to remove image,then follow below steps:
delete all records from

  1. catalog_product_entity_media_gallery
  2. catalog_product_entity_media_gallery_value'

table because magento is save all product image data at those table.

Then index from Index management from admin for set image black.

Then remove image from dir then goto Your magento dir at media/catalog/product and from this folder delete all file.

Another Process:

Andy Simpson,you need a script which is delete all product from your system which will delete from DB and file system.

Step1: Create a php at root direct of magento system which include Mage.php at first code.

require_once "YOURMAGENTODIR/app/Mage.php";
umask(0);

Step2: set current store is admin and set Developer mode

Mage::app('admin');
Mage::setIsDeveloperMode(true);

Step3: Get Product Collection and create a loop for get one product by one by one

$productCollection=Mage::getResourceModel('catalog/product_collection');

Step4: fetch product image by one and remove image one using below code:

$remove=Mage::getModel('catalog/product_attribute_media_api')->remove($product->getId(),$eachImge['file']);

FULL CODE:

<?php
require_once "YOURMAGENTODIR/app/Mage.php";
umask(0);
Mage::app('admin');
Mage::setIsDeveloperMode(true);

$productCollection=Mage::getResourceModel('catalog/product_collection');
foreach($productCollection as $product){
echo $product->getId();
echo "<br/>";
         $MediaDir=Mage::getConfig()->getOptions()->getMediaDir();
        echo $MediaCatalogDir=$MediaDir .DS . 'catalog' . DS . 'product';
echo "<br/>";

$MediaGallery=Mage::getModel('catalog/product_attribute_media_api')->items($product->getId());
echo "<pre>";
print_r($MediaGallery);
echo "</pre>";

    foreach($MediaGallery as $eachImge){
        $MediaDir=Mage::getConfig()->getOptions()->getMediaDir();
        $MediaCatalogDir=$MediaDir .DS . 'catalog' . DS . 'product';
        $DirImagePath=str_replace("/",DS,$eachImge['file']);
        $DirImagePath=$DirImagePath;
        // remove file from Dir

        $io     = new Varien_Io_File();
         $io->rm($MediaCatalogDir.$DirImagePath);

        $remove=Mage::getModel('catalog/product_attribute_media_api')->remove($product->getId(),$eachImge['file']);
    }


}
深海蓝天 2024-11-09 01:01:37

如果您想删除更多产品(例如 1000 或 5000 个),请使用直接查询的底部一项。

在尝试此操作之前,请先备份您的数据库

<?php
require_once 'app/Mage.php';
umask(0);
Mage::app();
set_time_limit(0);
ini_set('display_error','1');


$resource = Mage::getSingleton('core/resource');
$connection = $resource->getConnection('core_write');

$id = 101;
$product = Mage::getModel('catalog/product')->load($id);

$q = "DELETE FROM `catalog_product_entity_media_gallery` where entity_id = '".$product->getId()."'"; 

$connection->query($q);

?>

If you want to remove more products like 1000 or 5000 then use bottom one with direct query.

Before try this one take backup of your database

<?php
require_once 'app/Mage.php';
umask(0);
Mage::app();
set_time_limit(0);
ini_set('display_error','1');


$resource = Mage::getSingleton('core/resource');
$connection = $resource->getConnection('core_write');

$id = 101;
$product = Mage::getModel('catalog/product')->load($id);

$q = "DELETE FROM `catalog_product_entity_media_gallery` where entity_id = '".$product->getId()."'"; 

$connection->query($q);

?>
坐在坟头思考人生 2024-11-09 01:01:37

访问数据库进行此类操作是一个坏主意。
您可以在自定义模块中使用以下代码片段删除图像(或通过更改“位置”值重新排序它们)(为了简单起见,我使用 ObjectManager,但您应该将 ProductFactory 和 ProductRepositoryInterface 放在构造函数中)

$objectManager = ObjectManager::getInstance();

//Get ProductFactory in order to instatiate the Product Object 
$productFactory = $objectManager->get('\Magento\Catalog\Model\ProductFactory');

//We have to use ProductRepositoryInterface in order to save the changes bellow to the product
$productRepository = $objectManager->get('\Magento\Catalog\Api\ProductRepositoryInterface');

//Load a Product by ID in this case 1
$product = $productFactory->create()->load(1);

//Gets an array containing arrays representing each image in the gallery
$mediaGalleryEntries = $product->getMediaGalleryEntries();

//Here we delete every image, you can use conditions in foreach
foreach ($mediaGalleryEntries as $key => $imageEntry) {
    unset($mediaGalleryEntries[$key]);
}

//Finally set the altered entries and save using productRepository
$product->setMediaGalleryEntries($mediaGalleryEntries);
$productRepository->save($product);

上面的代码在 Magento 2.3 中进行了测试,它还会从文件系统中删除图像。您可以使用它来删除或更改条目(更改位置、图像角色等)

要查看每个 imageEntry,您可以使用 $imageEntry->getData() 访问其数据

Accessing the Database for such things is a bad idea.
You can delete images ( or reorder them by changing the 'position' value ) using the following snippet in a custom module ( for the sake of simplicity i'm using ObjectManager, but you should place ProductFactory and ProductRepositoryInterface in the constructor )

$objectManager = ObjectManager::getInstance();

//Get ProductFactory in order to instatiate the Product Object 
$productFactory = $objectManager->get('\Magento\Catalog\Model\ProductFactory');

//We have to use ProductRepositoryInterface in order to save the changes bellow to the product
$productRepository = $objectManager->get('\Magento\Catalog\Api\ProductRepositoryInterface');

//Load a Product by ID in this case 1
$product = $productFactory->create()->load(1);

//Gets an array containing arrays representing each image in the gallery
$mediaGalleryEntries = $product->getMediaGalleryEntries();

//Here we delete every image, you can use conditions in foreach
foreach ($mediaGalleryEntries as $key => $imageEntry) {
    unset($mediaGalleryEntries[$key]);
}

//Finally set the altered entries and save using productRepository
$product->setMediaGalleryEntries($mediaGalleryEntries);
$productRepository->save($product);

The code above tested in Magento 2.3 and it also removes images from the filesystem. You can use it to either delete or alter an entry ( change position, image roles etc )

To view each imageEntry you can access its data using $imageEntry->getData()

魂牵梦绕锁你心扉 2024-11-09 01:01:37

不久前我不得不做类似的事情 - 需要在导入过程中替换所有产品图像。

此链接有很大帮助:http://www.sharpdotinc.com/mdost/2010/03/02/magento-import-multiple-images-or-remove-images-durring-batch-import/

希望它会给你一个正确的方向推动。

I had to do something similar not too long ago - needed to replace all product images during an import.

This link helped a lot: http://www.sharpdotinc.com/mdost/2010/03/02/magento-import-multiple-images-or-remove-images-durring-batch-import/

Hopefully it will give you a nudge in the right direction.

相思故 2024-11-09 01:01:37
DELETE FROM catalog_product_entity_media_gallery WHERE value_id NOT IN (SELECT * FROM (SELECT MIN(value_id) FROM `catalog_product_entity_media_gallery` GROUP BY LEFT(value,17), entity_id  having count(*) > 1) x)

为什么是17?

您重复的例子如下:

/0/0/000116810609_1.jpg

/0/0/000116810609_2.jpg

/0/0/000116810609_3.jpg

/0/0/000116810609_4.jpg

LEFT(value,17)

/0/0/000116810609

/0/0 /000116810609

/0/0/000116810609

/0/0/000116810609

不,它们是重复的;)

DELETE FROM catalog_product_entity_media_gallery WHERE value_id NOT IN (SELECT * FROM (SELECT MIN(value_id) FROM `catalog_product_entity_media_gallery` GROUP BY LEFT(value,17), entity_id  having count(*) > 1) x)

why 17 ?

The expamle you have duplicate like this:

/0/0/000116810609_1.jpg

/0/0/000116810609_2.jpg

/0/0/000116810609_3.jpg

/0/0/000116810609_4.jpg

LEFT(value,17)

/0/0/000116810609

/0/0/000116810609

/0/0/000116810609

/0/0/000116810609

No they are duplicates ;)

小忆控 2024-11-09 01:01:37

将以下脚本放入magento根目录并执行

 <?php

  require_once("app/Mage.php");
  umask(0);
   Mage::app();
   $ob = new Clean();
   $ob->removemedia();

 Class Clean {

 function removemedia() {
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$select = $read->select()
        ->from('catalog_product_entity_media_gallery', '*')
        ->group(array('value_id'));

$flushImages = $read->fetchAll($select);
echo count($flushImages);
$array = array();
foreach ($flushImages as $item1) {
    $array[] = $item1['value'];
}

$valores = $array;

$pepe = 'media' . DS . 'catalog' . DS . 'product';

$leer = $this->listDirectories($pepe);

foreach ($leer as $item) {
    try {
        $item = strtr($item, '\\', '/');
        if (!in_array($item, $valores)) {
            $valdir[]['filename'] = $item;
            unlink('media/catalog/product' . $item);
        }
    } catch (Zend_Db_Exception $e) {

    } catch (Exception $e) {
        //Mage::log($e->getMessage());
    }
  }
 }

function listDirectories($path) {
    if (is_dir($path)) {
       if ($dir = opendir($path)) {
        while (($entry = readdir($dir)) !== false) {
            if (preg_match('/^\./', $entry) != 1) {
                if (is_dir($path . DS . $entry) && !in_array($entry, array('cache', 'watermark'))) {
                    $this->listDirectories($path . DS . $entry);
                } elseif (!in_array($entry, array('cache', 'watermark')) && (strpos($entry, '.') != 0)) {
                    $this->result[] = substr($path . DS . $entry, 21);
                }
            }
        }
        closedir($dir);
    }
 }
 return $this->result;
 }

  }

Place the below script in magento root and execute

 <?php

  require_once("app/Mage.php");
  umask(0);
   Mage::app();
   $ob = new Clean();
   $ob->removemedia();

 Class Clean {

 function removemedia() {
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$select = $read->select()
        ->from('catalog_product_entity_media_gallery', '*')
        ->group(array('value_id'));

$flushImages = $read->fetchAll($select);
echo count($flushImages);
$array = array();
foreach ($flushImages as $item1) {
    $array[] = $item1['value'];
}

$valores = $array;

$pepe = 'media' . DS . 'catalog' . DS . 'product';

$leer = $this->listDirectories($pepe);

foreach ($leer as $item) {
    try {
        $item = strtr($item, '\\', '/');
        if (!in_array($item, $valores)) {
            $valdir[]['filename'] = $item;
            unlink('media/catalog/product' . $item);
        }
    } catch (Zend_Db_Exception $e) {

    } catch (Exception $e) {
        //Mage::log($e->getMessage());
    }
  }
 }

function listDirectories($path) {
    if (is_dir($path)) {
       if ($dir = opendir($path)) {
        while (($entry = readdir($dir)) !== false) {
            if (preg_match('/^\./', $entry) != 1) {
                if (is_dir($path . DS . $entry) && !in_array($entry, array('cache', 'watermark'))) {
                    $this->listDirectories($path . DS . $entry);
                } elseif (!in_array($entry, array('cache', 'watermark')) && (strpos($entry, '.') != 0)) {
                    $this->result[] = substr($path . DS . $entry, 21);
                }
            }
        }
        closedir($dir);
    }
 }
 return $this->result;
 }

  }
琉璃梦幻 2024-11-09 01:01:36

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

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 magentocommerce.com/wiki/doc/webservices-api/api/catalog_product_attribute_media

太糟糕了,它不像 $product->getImages() 那么简单,嗯?

Okay, this is how I finally fixed my problem.

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']);
}

This is the link that finally set my head straight: http://www.magentocommerce.com/wiki/doc/webservices-api/api/catalog_product_attribute_media

Too bad it's not as simple as $product->getImages(), eh?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文