I have some thousand products and want to find all products without an image. I tried to search for (no image) in the admin products grid, but no result. How can I make an S
also, to get the sql that the query Alan describes runs behind the scenes:
echo (string) $products->getSelect();
There are both ways to do:
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('small_image',array('notnull'=>'','neq'=>'no_selection'));
Above code should work but in my case it was not working. So i tried following:
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('small_image',array('neq'=>'no_selection'));
Good Luck!
I wrote up a blog article a little while back with a sql query to find missing images. It doesn't disable products, but it's a start at least: http://prattski.com/2010/06/29/magento-sql-to-find-missing-images/. Should be pretty easy to do from this point. You may have to change the attribute ID if yours doesn't happen to match mine.
I tried all but this works for me when flat catalog is enable
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter(
array(
array(
'attribute' => 'image',
'null' => '1'
),
array(
'attribute' => 'small_image',
'null' => '1'
),
array(
'attribute' => 'thumbnail',
'null' => '1'
),
array(
'attribute' => 'image',
'nlike' => '%/%/%'
),
array(
'attribute' => 'small_image',
'nlike' => '%/%/%'
),
array(
'attribute' => 'thumbnail',
'nlike' => '%/%/%'
)
),
null,
'left'
);
I only want to add that the answer of Sean Michaud is right except we don't need to use that
array (
'attribute' => 'image', // null fields
'null' => true
),
Using that page: http://bytes.com/topic/sql-server/answers/83267-wildcard-doesnt-match-using-like-varchar-field-wierd
"A NULL value is not the same as a string of zero length. NULL represents the absence of any value, and the SQL standard says that a NULL value is never equal to any other value including another NULL"
So %/%/%
won't get the NULL values, but adding the code from above we will fix the error and get the image fields with NULL values. This is the result
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter(array(
array (
'attribute' => 'image', // null fields
'null' => true
),
array (
'attribute' => 'image', // check for information that doesn't conform to Magento's formatting
'nlike' => '%/%/%'
),
));
If you want to work with all image attributes, the code could be like this
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter(array(
array (
'attribute' => 'image', //Check for information that doesn't conform to Magento's formatting
'nlike' => '%/%/%'
),
array (
'attribute' => 'small_image', //Check for information that doesn't conform to Magento's formatting
'nlike' => '%/%/%'
),
array (
'attribute' => 'thumbnail', //Check for information that doesn't conform to Magento's formatting
'nlike' => '%/%/%'
),
array (
'attribute' => 'image', //Check for null fields
'null' => true
),
array (
'attribute' => 'small_image', //Check for null fields
'null' => true
),
array (
'attribute' => 'thumbnail', //Check for null fields
'null' => true
),
));
I tried all of these answers in various combinations, and only got a small subset of my catalogue returned. The reason: I originally imported my products using a bespoke product image import script.
If I didn't specify images for some rows during import, the script did not create NULL
or empty attribute values for those images. It simply did not create the attribute rows at all.
Since addAttributeToFilter
uses an INNER
join by default and there was no image attribute value to join to, the queries posted here didn't catch those SKUs.
The code below returns all products for which image, small_image or thumbnail are null, incorrectly formatted, or the row is missing entirely.
The third parameter to addAttributeToFilter
allow you to specify the type of join to be used in conjunction with the OR
clauses of the WHERE
statement.
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter(
array(
array(
'attribute' => 'image',
'null' => '1'
),
array(
'attribute' => 'small_image',
'null' => '1'
),
array(
'attribute' => 'thumbnail',
'null' => '1'
),
array(
'attribute' => 'image',
'nlike' => '%/%/%'
),
array(
'attribute' => 'small_image',
'nlike' => '%/%/%'
),
array(
'attribute' => 'thumbnail',
'nlike' => '%/%/%'
)
),
null,
'left'
);
If, like me, you want to convert this to a SQL query to export as a CSV from your SQL client, simply print the query from the PHP script:
echo $products->getSelect();
I've seen some SQL posted on StackOverflow which hard-codes the attribute_id
integers that refer to the image
, small_image
and thumbnail
attributes, but these can differ from one install to another. In Magento, querying with the ORM is much better than with the SQL.