1

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. – Nguyen Cong Binh Jan 11 '18 at 9:29
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');
  • Hey..you saved my day...Thank you so much.... – Sneha Panchal Jan 11 '18 at 13:18
2

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

1

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.