Index.php (Controller File)

$productCollection = $_objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollection->create();

I want to check image is null or not I also check the filter of no_selection but its not working.

So I want to try the media_gallery attribute filter.

Please Help If You Know About This.

Thanks In Advance!

  • I think you should check image is null or not at block file or phtml. Jan 11 '18 at 9:29

I know this is an old question, but Magento 2 has changed a bit and an easier way (imho) is using the addMediaGalleryData() method of the Magento\Catalog\Model\ResourceModel\Product\Collectionclass. This works at least in Magento 2.1.9

  • Here's a pretty Magento2 command: $collection = $block->getLoadedProductCollection()->addMediaGalleryData();
    – DarthRez
    Feb 12 '20 at 15:55
  • does it have any effect?getLoadedProductCollection() normally gets a collection that is already loaded, which means adding filters shouldn't do much. And if it does, I assume that the collection gets loaded twice, which is bad for performance. Feb 12 '20 at 15:57

I got the Answer to my Question:

If you want to filter product collection which has no any images, so I just do some custom code on which I got the success. I hope you will get the solution from this answer.

Magento2 have 2 tables which store the value of uploaded product image

  1. catalog_product_entity_media_gallery_value
  2. catalog_product_entity_media_gallery

One table store the value_id of each products and second table contain the image url as value.

So we have to add joins in the collection to know which product have images, according to that rest of the products have no images.

In my controller file

$productCollection = $_objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollection->create();

/*----Join Query for getting value id from media_gallery_value table----- */

     array('value_entity' => $collection->getTable('catalog_product_entity_media_gallery_value')),
     'e.entity_id = value_entity.entity_id',

/*----Join Query for getting value from media_gallery table using value_id----- */

    array('value_data' => $collection->getTable('catalog_product_entity_media_gallery')),
    'value_entity.value_id = value_data.value_id',

/*----Here, you can get the unique product ids wich have images ----- */


/*---- $newCollection have product which have no images ----- */

$newCollection = $productCollection->create()

echo '<pre>';print_r($newCollection->getData());die('died');
  • Hey..you saved my day...Thank you so much.... Jan 11 '18 at 13:18

Make sure to add addMediaGalleryData() after you've set all filter of your collection, because else it won't take them into account.

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.