Here is a simple code for those who want to delete all images from the product for which images are not exists in the media/catalog/product folder
[php]
<pre><em id="__mceDel">
ini_set('memory_limit', '-1');
set_time_limit(0);
require_once ('app/Mage.php');
Mage::app('default');
$imgfolder = 'media/catalog/product';
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*');
foreach ($collection as $product) {
$img = $product->getImage();
if(!file_exists($imgfolder.$img)){
$products = Mage::getModel('catalog/product')->load($product->getId());
$entityTypeId = $products->getEntityTypeId();
$mediaGalleryAttribute = Mage::getModel('catalog/resource_eav_attribute')->loadByCode($entityTypeId, 'media_gallery');
$gallery = $products->getMediaGalleryImages();
foreach ($gallery as $image)
$mediaGalleryAttribute->getBackend()->removeImage($products, $image->getFile());
$products->save();
}
}
[/php]
Explanation of code :
1) ini_set('memory_limit', '-1');
There could be thousand of products that we can not load them in one shot.
2) set_time_limit(0);
Setting script execution time limit to no-limit as it could stop in between
No comments:
Post a Comment