image - 删除magento中未使用的产品图片

标签 image magento product

image-clean模块在/media/catalog/product 下列出未使用的图像,并让您删除它们。
是否有脚本无需用户交互即可自动删除未使用的图像?
我想手动运行这个脚本或每晚使用一个 cron 作业。

谢谢

最佳答案

如果您查看该模块的管理 Controller 的源代码,您可以看到他们用于执行批量删除的代码

#File: app/code/local/Mage/Imaclean/controllers/Adminhtml/ImacleanController.php
public function massDeleteAction() {
    $imacleanIds = $this->getRequest()->getParam('imaclean');
    if(!is_array($imacleanIds)) {
        Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select item(s)'));
    } else {
        try {
            $model = Mage::getModel('imaclean/imaclean');
            foreach ($imacleanIds as $imacleanId) {
                $model->load($imacleanId);
                unlink('media/catalog/product'. $model->getFilename());
                $model->setId($imacleanId)->delete();
            }
            Mage::getSingleton('adminhtml/session')->addSuccess(
                Mage::helper('adminhtml')->__(
                    'Total of %d record(s) were successfully deleted', count($imacleanIds)
                )
            );
        } catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
        }
    }
    $this->_redirect('*/*/index');
}

因此,此 Controller 操作接受多个“imaclean/imaclean”模型 id,使用这些 id 执行删除。因此,该操作中的关键代码是
$imacleanIds = $this->getRequest()->getParam('imaclean');
$model = Mage::getModel('imaclean/imaclean');
foreach ($imacleanIds as $imacleanId) {
    $model->load($imacleanId);
    unlink('media/catalog/product'. $model->getFilename());
    $model->setId($imacleanId)->delete();
}

因此,您可以在独立版本中复制上述代码,例如
//itterates through all 'imaclean/imaclean' models in the database
$models = Mage::getModel('imaclean/imaclean')->getCollection();
foreach ($models as $model) {
    unlink('media/catalog/product'. $model->getFilename());
    $model->setId($model->getId())->delete();
}

最后,看起来像“imaclean/imaclean”模型用于跟踪哪些图像不再需要。看起来模块创建了这些(即运行检查未使用的图像),在带有 compareList 的 newAction 中默认助手的方法。
public function newAction(){    
    Mage::helper('imaclean')->compareList();
    $this->_redirect('*/*/');
}

因此,我们可以将它添加到脚本的开头,以及事实上的 Magento 初始化,这应该可以满足我们的需要。
#File: cleanup.php
require_once "app/Mage.php";
$app = Mage::app("default");

Mage::helper('imaclean')->compareList();
$models = Mage::getModel('imaclean/imaclean')->getCollection();
foreach ($models as $model) {
    unlink('media/catalog/product'. $model->getFilename());
    $model->setId($model->getId())->delete();
}   

这至少应该让你开始。祝你好运!

关于image - 删除magento中未使用的产品图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4339699/

相关文章:

image - 将白色 png 图像更改为特定颜色

jquery - 设置从 JQUERY/FLICKR API 提取的缩略图图像的自定义大小

css - 升级到 1.8.1 后,Magento CSS/JS 未加载

magento - Magento:重写 block 不起作用

rest - Magento 2 Rest Api - 请求参数中的关联数组

php - Woocommerce:如何在购物车中只允许一种产品类型?

javascript - 删除 Magento 2 中的默认产品库

python - Pygame 点击图像(不是矩形)

matlab - 显示目录中的随机图像对

php - WooCommerce 根据添加的自定义 header 字段过滤管理订单列表