Magento – Magento 2: How to programatically mark product image as hidden from product page

imagemagento-2.1.3productproduct-images

I'm trying to programatically set the "Hide from Product Page" setting on an image.

The setting can be found here: Products > Catalog > (select a product) > Images And Videos > (click on an image).

Image showing the setting.

So far, the only way I've been able to change that setting, is by doing it manually.

Here's how I currently add images to my products:

$product->addImageToMediaGallery($imagePath, ['thumbnail'], false, true);
$product->save();

I thought that the last parameter which is called exclude and described as mark image as excluded from product page view would do the trick, but it doesn't. Regardless of if I set the value to trueor false, the image remains visible on the product page and the checkbox is unchecked.

I've been banging my head against a wall for the better part of a day now, so any suggestions will be greatly appreciated.

Best Answer

You can simply change product image visibility using a database.

If you want disabled all product images so run the below query in the database.

UPDATE `catalog_product_entity_media_gallery_value` SET `disabled` = '1' WHERE `disabled` = '0';

If you want to enable all product images so run the below query in the database.

UPDATE `catalog_product_entity_media_gallery_value` SET `disabled` = '0' WHERE `disabled` = '1';

Thanks

Related Topic