4

i try to filter product collection to get a product collection which only has at least one image like this:

  $collection->addAttributeToFilter('image', array("neq"=>'no_selection'));
  $collection->addAttributeToFilter('small_image', array("neq"=>'no_selection'));

but it's not working at all, it still shows the product with the placeholder image

1
  • 2
    Did you get solution for your problem by any of below answers? If you are facing any issue with any of the answer, you can add comment for the same. – Jaimin Sutariya Jul 25 '17 at 8:15
1

Other idea ... get all unique entity_id (product Ids) from catalog_product_entity_media_gallery table and use result to filter collection.

$resource = Mage::getSingleton('core/resource');
$read     = $resource->getConnection('core_read');
$query    = $read->select()
    ->from($resource->getTableName('catalog_product_entity_media_gallery'), array('entity_id'))
    ->distinct(true)
    ->group('entity_id');
$result = $read->query($query);

$productIds = array();
while ($row = $result->fetch()) {
    $productIds[] = $row['entity_id'];
}

For your collection

$collection->addIdFilter($productIds);

Edit:

$collection->addAttributeToSelect('small_image, thumbnail, image');
$collection->addAttributeToFilter(
    array(
        array('attribute' => 'image','neq' => 'no_selection'),
        array('attribute' => 'small_image','neq' => 'no_selection'),
        array('attribute' => 'thumbnail','neq' => 'no_selection'),
    )
);

Note: for performance I'd use first approach.

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.