Magento后台管理同时删除产品和产品图片

Magento默认删除产品的时候,产品图片不会同时被删除,这样的话,

如果经常要删除产品,服务器上会保存很多没用的产品图片,

我们可以通过重写Magento 的 Product 模块属性,来达到同时删除产品和图片的功能

新建个文件 app/code/local/Mage/Catalog/Model/Product.php,

复制 app/code/core/Mage/Catalog/Model/Product.php 文件里的代码,

将原有的delete函数:

public function delete()
{
    parent::delete();
    Mage::dispatchEvent($this->_eventPrefix.'_delete_after_done', array($this->_eventObject=>$this));
    return $this;
}

替换成:

public function delete() {
    foreach ($this->getMediaGallery('images') as $image) {
        $image_path = $this->getMediaConfig()->getMediaPath($image['file']);
        if (file_exists($image_path)) {
            @unlink($image_path);
        }
    }
    parent::delete();
    Mage::dispatchEvent($this->_eventPrefix . '_delete_after_done', array($this->_eventObject => $this));
    return $this;
}

标签


Warning: error_log(/www/wwwroot/www.ctrol.cn/wp-content/plugins/spider-analyser/#log/log-0212.txt): failed to open stream: No such file or directory in /www/wwwroot/www.ctrol.cn/wp-content/plugins/spider-analyser/spider.class.php on line 2900