Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

SOLVED Remove images physically
 
tsifra
Member
 
Total Posts:  57
Joined:  2008-09-14
 

Hi!

I’m trying to force magento to remove product images when they are delete from database.

So:
1. Edit product page, remove chtckboxes & Save - ok
2. Product list, checkboxes & submit delete action - ok
3. Edit product page, delete button on the top of the page - not working

So, about 3..

I have override class

class Peerus_Catalog_Model_Product extends Mage_Catalog_Model_Product
...

with overridden handler

protected function _beforeDelete()
{
    
if(!$this->hasData('media_gallery_images') && is_array($this->getMediaGallery('images'))) {
        
foreach ($this->getMediaGallery('images') as $image{
            
//remove code
         
}
    }
}

So this handler works fine for case 2, but don’t work for case 3. Any ideas?

 
Magento Community Magento Community
Magento Community
Magento Community
 
tsifra
Member
 
Total Posts:  57
Joined:  2008-09-14
 

FIXED.

All images related with product are deleted when they are deleted from database. At least feats my needs today.

Overrides

\Catalog\Model\Product.php

class Mycompany_Catalog_Model_Product extends Mage_Catalog_Model_Product
{
    
protected function _beforeDelete()
    
{
//Mycompany
        
$delproduct null;
        if(!
$this->hasData('media_gallery_images') && is_array($this->getMediaGallery('images'))) {
            $delproduct 
$this;
        
else {
            $delproduct 
= new Mage_Catalog_Model_Product();
            
$delproduct->load($this->getId());
        
}
        
        
if(!$delproduct->hasData('media_gallery_images') && is_array($delproduct->getMediaGallery('images'))) {
            
foreach ($delproduct->getMediaGallery('images') as $image{
                $this
->rmFile($this->getMediaConfig()->getMediaPath($image['file']));
            
}
        }
//EOF Mycompany

        //$this->cleanCache();
        //$this->_protectFromNonAdmin();
        
return parent::_beforeDelete();
    
}
    
//Mycompany
    
protected function rmFile($file)
    {
        $ioAdapter 
= new Varien_Io_File();
        try 
{
            $ioAdapter
->open(array(
                
'path'=>dirname($file)
            ));
            
$ioAdapter->rm($file);
        
}
        
catch (Exception $e{
            Mage
::throwException(Mage::helper('catalog')->__('Failed to remove file: %s'$e->getMessage()));
        
}
    }
//EOF Mycompany
}

\Catalog\Model\Product\Attribute\Backend\Media.php

class Mycompany_Catalog_Model_Product_Attribute_Backend_Media
    
extends Mage_Catalog_Model_Product_Attribute_Backend_Media
{
    
public function afterSave($object)
    
{
        
if ($object->getIsDuplicate() == true{
            $this
->duplicate($object);
            return;
        
}

        $attrCode 
$this->getAttribute()->getAttributeCode();
        
$value $object->getData($attrCode);
        if (!
is_array($value) || !isset($value['images'])) {
            
return;
        
}
        $toDelete 
= array();
        
$filesToValueIds = array();
//Mycompany
    
$arr_files_todel = array();
//EOF Mycompany
        
foreach ($value['images'as &$image{
            
if(!empty($image['removed'])) {
                
if(isset($image['value_id'])) {
                    $toDelete[] 
$image['value_id'];
//Mycompany
                    
$arr_files_todel[] $image['file'];
//EOF Mycompany
                
}
                
continue;
            
}

            
if(!isset($image['value_id'])) {
                $data 
= array();
                
$data['entity_id']      $object->getId();
                
$data['attribute_id']   $this->getAttribute()->getId();
                
$data['value']          $image['file'];
                
$image['value_id']      $this->_getResource()->insertGallery($data);
            
}

            $this
->_getResource()->deleteGalleryValueInStore($image['value_id']$object->getStoreId());

            
// Add per store labels, position, disabled
            
$data = array();
            
$data['value_id'$image['value_id'];
            
$data['label']    $image['label'];
            
$data['position'= (int) $image['position'];
            
$data['disabled'= (int) $image['disabled'];
            
$data['store_id'= (int) $object->getStoreId();

            
$this->_getResource()->insertGalleryValueInStore($data);
        
}

        $this
->_getResource()->deleteGallery($toDelete);

//Mycompany
        
foreach ($arr_files_todel as $file_todel{
            $this
->rmFile($this->_getConfig()->getMediaPath($file_todel));
        
}
//EOF Mycompany
    
}
//Mycompany
    
protected function rmFile($file)
    {
        $ioAdapter 
= new Varien_Io_File();
        try 
{
            $ioAdapter
->open(array(
                
'path'=>dirname($file)
            ));
            
$ioAdapter->rm($file);
        
}
        
catch (Exception $e{
            Mage
::throwException(Mage::helper('catalog')->__('Failed to remove file: %s'$e->getMessage()));
        
}
    }
//EOF Mycompany
}
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top