0

I want to mark some of the product images as excluded against some products. I tried this link http://www.magecoders.com/blog/2013/05/programmatically-exclude-product-images-in-magento/ so far. But didn't work.

0

I had the same issue. I managed to exclude the images by removing them using the same code but with a small change. Before doing any of the following, BACKUP your database.

Change this

$mediaApi->update($_product->getId(),$item['file'],$data);

To this

$mediaApi->remove($_product->getId(),$item['file']);

It will remove the image from displaying in admin, but the actual image will remain under media folder.

Regards, Daniel

0

Try this code:

<?php
require_once('app/Mage.php');
Mage::app('admin');

class ITEMS
{
    public function index()    
    {
        $productIds = Mage::getResourceModel('catalog/product_collection')->getAllIds();
        if(is_array($productIds)) {
            foreach($productIds as $productId) {
                $product = Mage::getModel('catalog/product')->load($productId);
                //get all images
                $mediaGallery = $product->getMediaGallery();
                $attributes = $product->getTypeInstance(true)->getSetAttributes($product);
                $mediaGalleryAttribute = $attributes['media_gallery'];
                //if there are images
                if (isset($mediaGallery['images'])){
                    //loop through the images
                    foreach ($mediaGallery['images'] as $image){
                        $mediaGalleryAttribute->getBackend()->updateImage($product, $image['file'], array('exclude' => true));
                    }
                    echo "Product ".$product->getSku()." images excluded <br>";
                    $product->save();
                }       
            }
        }
    }
}

$obj = new ITEMS();
$obj->index();
?> 
0

I am Successfully able to exclude the images by simple below code.

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$this->_productSku);

if ($product->getId()){
     $mediaApi = Mage::getModel("catalog/product_attribute_media_api");
     $items = $mediaApi->items($product->getId());
     foreach($items as $item){
         $mediaApi->remove($product->getId(), $item['file']);
         //$mediaApi->save();
     }
}
    $product->save();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.