How can I find all products without images in Magento?

前端 未结 10 709
Happy的楠姐
Happy的楠姐 2020-12-18 03:39

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

10条回答
  •  旧巷少年郎
    2020-12-18 03:55

    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.

提交回复
热议问题