Search code examples
phpmagentofilteringmagento-1.8product

Filter products without images in Magento


Requrement: We have a bunch of products with and without images. We only need to display products with images in the frontend.

What I did: I have tried almost all the SO resources like:

Filter products without images on Magento frontend

How can I find all products without images in Magento?

Hide Products without images magento

and their various combinations with no luck.. I think something changed in the new version of Magento. I am using version 1.8.1.0

Is somebody able to shed some lights here?

UPDATE: On my debugging, I found out that

->addAttributeToFilter('small_image', array('neq' => 'no_selection'))

really worked. But an error occurs in layered price filter. I was thinking this is not working earlier.I am getting the error similar to: Magento 1.7 price filter error (Column not found: 1054 Unknown column 'e.min_price' in 'where clause')...

According to Royw's answer to the above question, I believe, we need to edit the file: Mage/Catalog/Model/Layer/Filter/price.php


Solution

  • This can be done using Magento events. I added an observer for the event:

    catalog_product_collection_load_before

    which adds a filter to remove all the products without images when loading product collection. The observer is as follows:

    public function filterProductsWithoutImages($observer) {
        if (isset($observer['collection'])) {
            $collection = $observer['collection'];
            $collection->addAttributeToFilter('small_image', array('neq' => "no_selection"));
            return $this;
        }
    }
    

    Here we need to disable "Use Flat Catalog Product". For that:

    Admin > Configuration > Catalog > Frontend > Use Flat Catalog Product to "No"

    Since this filtering is not compatible with the use of "Flat Catalog Product"

    Product counts in search pages work fine with this.