3

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!

1
  • I think you should check image is null or not at block file or phtml. Commented Jan 11, 2018 at 9:29

3 Answers 3

5

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

2
  • Here's a pretty Magento2 command: $collection = $block->getLoadedProductCollection()->addMediaGalleryData();
    – DarthRez
    Commented Feb 12, 2020 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. Commented Feb 12, 2020 at 15:57
3

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

2

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----- */

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

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

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

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

$collection->getSelect()
     ->reset(\Zend_Db_Select::COLUMNS)
     ->columns('entity_id')        
     ->group(array('entity_id'));

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

$newCollection = $productCollection->create()
              ->addAttributeToFilter('entity_id',['nin'=>$collection->getData()]);

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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