Magento – Hide products without images (CE 1.9)

magento-1.9product-images

This question has been asked multiple times, but none of them have a clear solution and those few cases who should work (according to authors) are definitely not working properly in the current version of Magento, at least for us.

I have setup the following code based on various snippets from overflow and magento stackexchange:

require_once('app/Mage.php');

Mage::app('default');
Mage::app ()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('image', array('neq' => 'no_selection'));

$i = 0;
foreach($products as $product)
{
    echo  $i++.": ".$product->getSku() . " has no image \n<br />\n";
}

When I run this on Magento CE 1.9 on a large shop with thousands of configurable products, I get way more products than it should. The code presents me with a list of way over 1k products, a lot of them with working products with images (products without images should be less than half that figure).
Plus, I picked some random products from which I knew that they had no images and searched for their SKU in the result list from above's code: I couldn't find any of them.

tl/dr: this list is some product listing, but there is no easily discernable connection between the list and "products without images".

I am quite new to all this collection business and Magento in general.

What is the definite indicator of a product which clearly says: "I have no image"?

How would one need to change the code above to properly work with current iterations of Magento CE?

— UPDATE —

The following code provides me with a readable list of all products without images, but I can't use this code on the page to hide products without images. How can I translate this into an extended filter?

$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('type_id',array('eq'=>'configurable'));

$i = 1;
foreach($products as $product)
{
    if ($product->getImage() == NULL) {
        echo $i++.": ".$product->getSKU(). " - ".$product->getName()." has no image!<br />";
        echo "<hr />";
    }
}

A filter including all of this should probably look something like this:
However, this does not work. This simply generates me a list of all configurable products WITH images. I tried inversing the image-attribute related filters, but to no avail. The list does not change. What did I miss regarding the filters?

$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter(array(
    array (
        'attribute' => 'type_id',
        'eq' => 'configurable'
    ),
    array (
        'attribute' => 'image', // null fields
        'null' => true
    ),
    array (
        'attribute' => 'image', // empty, but not null
        'eq' => ''
    ),
    array (
        'attribute' => 'image',
        'like' => 'no_selection'
    )
));

Best Answer

You have to check both "no_selection" and "NULL", like

WHERE IFNULL(small_image, "no_selection")="no_selection"

So the condition the image is NULL you have "no_selection" and if it is equal to "no_selection"

The code: ...

$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('image', array('neq' => 'no_selection'));

$select = $products->getSelect();
$select->where(new Zend_Db_Expr('IFNULL(small_image, "no_selection")="no_selection"'));

....