6

I want to create a collection of all the products without a image and set them do the status "disabled".

Because i use a automatic import script there are some products that HAD images before, but don't have them anymore.

For that reason, there are images selected, but they don't exist anymore and the placeholder is shown on the frontend.

Is there anyway to still get the collection so i can give those product a new status?

This is what is tried and didn't work:

$_products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter(array(
        array (
            'attribute' => 'image',
            'like' => 'no_selection'
        ),
        array (
            'attribute' => 'image',
            'null' => true
        ),
        array (
            'attribute' => 'image',
            'eq' => ''
        ),
        array (
            'nlike' => '%/%/%'
        ),
    ));

Strange thing is that the image is empty (but selected) and still doesn't show up in the collection.

EDIT:

When doing a var_dump on a product that shows a placeholder on the frontend and has no images in the backend i still see the file URL to the non-existing images:

["media_gallery"]=>
  array(2) {
    ["images"]=>
    array(3) {
      [0]=>
      array(9) {
        ["value_id"]=>
        string(5) "49038"
        ["file"]=>
        string(19) "/E/2/E23812_1_1.jpg"
        ["product_id"]=>
        string(4) "4386"
        ["label"]=>
        string(0) ""
        ["position"]=>
        string(2) "13"
        ["disabled"]=>
        string(1) "0"
        ["label_default"]=>
        NULL
        ["position_default"]=>
        string(2) "13"
        ["disabled_default"]=>
        string(1) "0"
      }
      [1]=>
      array(9) {
        ["value_id"]=>
        string(5) "49039"
        ["file"]=>
        string(19) "/E/2/E23812_2_1.jpg"
        ["product_id"]=>
        string(4) "4386"
        ["label"]=>
        string(0) ""
        ["position"]=>
        string(2) "14"
        ["disabled"]=>
        string(1) "0"
        ["label_default"]=>
        NULL
        ["position_default"]=>
        string(2) "14"
        ["disabled_default"]=>
        string(1) "0"
      }
      [2]=>
      array(9) {
        ["value_id"]=>
        string(5) "49040"
        ["file"]=>
        string(17) "/E/2/E23812_3.jpg"
        ["product_id"]=>
        string(4) "4386"
        ["label"]=>
        string(0) ""
        ["position"]=>
        string(2) "15"
        ["disabled"]=>
        string(1) "0"
        ["label_default"]=>
        NULL
        ["position_default"]=>
        string(2) "15"
        ["disabled_default"]=>
        string(1) "0"
      }
    }

So is there a way to filter a collection for a image URL that links to a non-existing item?

EDIT:

Got it:

$_products = Mage::getModel('catalog/product')
                ->getCollection()
                ->addAttributeToSelect('image');

foreach($_products as $_product){

    clearstatcache();
    $file = '../media/catalog/product'.$_product->getImage();

    if(!file_exists($file)) {
        $_product->setStatus(2);
        $_product->save();
    }

} 

Thanks for the help, hope anybody else finds this post useful.

  • Welcome to Magento.SE! This is a great question. I'll keep an eye here and if there's still not an answer later I'll try to take a look. – philwinkle Sep 10 '15 at 16:41
  • @philwinkle thanks, hope somebody (or you) can help me with this – RoRo Sep 10 '15 at 17:26
2

If the product does not have any images in the Images tab, you need to join and filter by the media gallery attribute, image just holds the filename of the selected image, which is still set in your case.

But it looks to me as if the products actually have images and it's only the files that are missing. In this case it's impossible to figure that out using the collection, since the database does not know anything about the image files (assuming you don't use database storage for your images)

That means, if the product images look like this in the backend, you can't filter:

screenshot

Instead, you will have to walk through all products and check if the files exist, using file_exists() in PHP.

  • Thanks, this worked. i updated the original post with the working collection. – RoRo Sep 11 '15 at 11:20
2
$_products = Mage::getModel('catalog/product')
                    ->getCollection()
                    ->addAttributeToSelect('image');

foreach($_products as $_product){
    if($_product->getImage() == NULL) {
        echo $_product->getProductUrl();
    }
}

This way you can get the products without images and you can update status for those products.

  • I used you code in combination with the answer of @fschmengler and now it works. thanks a lot – RoRo Sep 11 '15 at 11:22

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.