共计 691 个字符,预计需要花费 2 分钟才能阅读完成。
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; | |
} |
正文完