Excluding product images progamatically
You can exclude any product image for a given product by using the updateImage
method from the model Mage_Catalog_Model_Product_Attribute_Backend_Media
if you know the filename of the image you want to exclude (filename is the complete path beginning with the slash below media/catalog/product). For example with the following code you can exclude all existing images from the media gallery for the product with id = 12345:
$product_id = '12345';
$product = Mage::getModel('catalog/product')->load($product_id);
$attributes = $product->getTypeInstance(true)->getSetAttributes($product);
if (isset($attributes['media_gallery'])) {
$mediaGalleryAttribute = $attributes['media_gallery'];
$media = $mediaGalleryAttribute->getBackend();
$mediaGalleryData = $product->getData('media_gallery');
if (isset($mediaGalleryData['images']) && is_array($mediaGalleryData['images'])) {
foreach ($mediaGalleryData['images'] as $image) {
$media->updateImage($product, $image['file'], ['exclude' => 1]);
}
$product->save();
}
}
Running this code before your $product->addImageToMediaGallery()
call, should do what you want - maybe it's usefull to add a little more error handling.
There is of course a faster way just by updating the catalog_product_entity_media_gallery_value
table, but the approach above is probably the better one for your requirement.
Just to be complete, here the SQL statement for the fastest and simplest solution since you can exclude all images for all your products in one statement:
update catalog_product_entity_media_gallery_value set disabled = 1
where value_id in (
select value_id from catalog_product_entity_media_gallery
where entity_id in (12345,12346) -- list your product_ids here
);