2

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'
    )
));
1

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"'));

....

  • The problem seems to have something to do with configurable products. Plus there seems to be a problem with the ">addAttributeToFilter" line. Following code also provides an empty list. The filter itself worked, I tried looking for product names and that went well. $products = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect(array('image')) ->addAttributeToFilter('image', array('eq' => '')) ->load(); – blackcud Oct 30 '14 at 16:25
  • didnt work for me. – blackcud Dec 4 '14 at 15:15
1

I got this working. Mine was version 1.8.0.1. I did these:

1.Added:

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

to line # 157 in /app/code/core/Mage/Catalog/Block/Product/List.php(For paging to reflect the filtering) and copied it to Folder: /app/code/local/Mage/Catalog/Block/Product/(local changes)

2.Then Admin > Configuration > Catalog > Frontend > Use Flat Catalog Product to "No"

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

Update: I have to make a couple of edits to reflect the count of products in the advanced search and search pages. (However I believe this step is dirty but works.)

Edited:

$size = $this->getSearchModel()->getProductCollection()->getSize();

to

$size = $this->getSearchModel()->getProductCollection()->addAttributeToFilter('small_image', array('neq' => "no_selection"))->getSize();

on line #90(function "getResultCount") in app/code/core/Mage/CatalogSearch/Block/Advanced/Result.php (For advanced search results count to reflect the filtering) and copied it to Folder: /app/code/local/Mage/CatalogSearch/Block/Advanced/(local changes)

AND

Edited:

$size = $this->_getProductCollection()->getSize();

to

$size = $this->_getProductCollection()->addAttributeToFilter('small_image', array('neq' => "no_selection"))->getSize();

on line #186(function "getResultCount") in app/code/core/Mage/CatalogSearch/Block/Result.php (For search results count to reflect the filtering) and copied it to Folder: /app/code/local/Mage/CatalogSearch/Block/(local changes)

  • Disabling flate catalog categories is quite a performance hog so I fear this is not an option in our case. – blackcud Nov 12 '14 at 15:13
  • yeah its a performance hog but in my project having 13K product, there is no visible hog.. And BTW Magento cache can help us here I believe – Binod - GoFundMonica Nov 12 '14 at 16:00
  • I disabled the cache, reindexed, disabled flat catalog product and all the usual jazz and it didnt work. On which platform did you get this to work? We are using CE 1.9 – blackcud Dec 4 '14 at 14:54
  • Its CE 1.8.0.1. I had an error when I did step #1(something like column 'small_image' is not found in a table) I tried debugging and checked the table and then I tried searching something like: what is the need of that table(I think). That's how I came to step #2. Hope this helps – Binod - GoFundMonica Dec 4 '14 at 19:19
1

This is a more correct answer. 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 also 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.

  • 1
    As nothing works and everybody uses a completely different version of Magento, I will try to implement this. – blackcud Mar 3 '15 at 14:33

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.