2

I have noticed in my backend that some products have broken image links. Thus, placeholder gets used on frontend.

I would like to get a list of products that has the placeholder as it's image on the frontend. I have written the following, but it is not working correctly:

    <?php 
error_reporting(E_ALL);
ini_set('display_errors', '1');

require_once('app/Mage.php');
Mage::app();

error_reporting(1);
set_time_limit(0);
ini_set('memory_limit', '2048M');

$_products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('visibility', 4)
    ->addAttributeToFilter('status', 1);

foreach($_products as $_product){
    if($_product->getImageUrl()){
        echo($_product->getImageUrl(). $_product->getSku()."</br>");
    }
}?>

Update of new code I have tried:

    <?php 
error_reporting(E_ALL);
ini_set('display_errors', '1');

require_once('app/Mage.php');
Mage::app();

error_reporting(1);
set_time_limit(0);
ini_set('memory_limit', '2048M');

$_products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('visibility', 4)
    ->addAttributeToFilter('status', 1);

foreach($_products as $_product){
    if (!file_exists(Mage::getModel('catalog/product_media_config') ->getMediaUrl( $_product->getImage()))) {
    echo $_product->getSku(). "<br/>";
}
        //echo(Mage::getModel('catalog/product_media_config') ->getMediaUrl( $_product->getImage())."</br>");

}?>

Still not doing what I need

  • have a look at stackoverflow.com/questions/3565377/… (Aaron Pollack's answer) – jscar May 15 '17 at 12:08
  • @jscar That is to find products without any images. Some image files look to have not been transferred over, so database points to image files that are not present, meaning placeholder is used. I need to just find products with broken images to fix – Vaishal Patel May 15 '17 at 12:12
2

Try below code.

foreach($_products as $_product){
    $allImages = Mage::getModel('catalog/product')->load($_product->getId())->getMediaGalleryImages();
    echo "Product ".$_product->getSku()."<br>";
    foreach($allImages as $_image){
        if (!file_exists($_image->getPath())) {
            echo "Missing Images <br>";
            echo $_image->getPath()."<br>";
        }
    }
    echo "<br>";
}

It will give you products with List of Missing Images. If there is no missing image, it will show only product sku.

  • Is there a way I could adapt this to get products with any image as placeholder ? – Vaishal Patel May 15 '17 at 12:34
  • @VaishalPatel, please check updated answer, it will display any image which is missing. – Jaimin Sutariya May 15 '17 at 12:50
  • shall give this a go now, thank you for the help thus far :) – Vaishal Patel May 15 '17 at 12:51

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.