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; }
保存,并重新刷新缓存即可。
原创文章,转载请注明:转载自Web开发笔记 | Magento后台管理同时删除产品和产品图片
本文链接地址:https://www.magentonotes.com/magento-delete-product-and-picture.html
Comments on this entry are closed.