How to Check If Product Images Exist or Not in Magento 2
“It is not your customer’s job to remember you. It is your obligation and responsibility to make sure they don’t have the chance to forget you.”
A store owner will not leave any stone unturned to grab the visitors’ attention, impress them, leverage conversion, and retention.
One of the methods to grab a visitor’s attention and impress them is by visually representing your product.
Your product image in the web store serves as the first impression on your customer’s mindset. It is an influential factor that pushes customers towards the purchase.
Product images and videos give an idea of the product’s features, appearance, and how it may add value to their life or solve their problem.
According to a study by Forbes, high-quality image sells. As customers cannot check your product in person, it is important to have a high-quality product image that delivers the actual idea of quality and features.
However, sometimes it may happen in Magento 2 stores that certain product images are missing due to one reason or other.
It won’t look good if your customer is about to purchase something and clicks on that product, but the image is not there and he ends up without making a purchase.
To prevent it, use the method to check If product images exist or not in Magento 2 and avoid abandonment of purchase.
Check out the below solution.
Method to Check If Product Images Exist or Not in Magento 2
Use below code in the root script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); ini_set('memory_limit', '5G'); error_reporting(E_ALL); use Magento\Framework\App\Bootstrap; require '../app/bootstrap.php'; $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $state = $objectManager->get('Magento\Framework\App\State'); $state->setAreaCode('frontend'); $storeManager = $objectManager->create('Magento\Store\Model\StoreManagerInterface'); $fp = fopen('Products_Has_Image.CSV', 'w+'); $csvHeader = array('SKU', 'with active image'); fputcsv($fp, $csvHeader, ","); $p = 0; while (1) { $p++; $products = $objectManager->create('Magento\Catalog\Model\Product') ->getCollection() ->addAttributeToSelect('*') ->setPageSize(50) ->setCurPage($p) ->setOrder('id', 'ASC'); foreach ($products as $product) { if ($product->getThumbnail() != '' && $product->getThumbnail() != 'no_selection') { $isImage = 'Y'; } else { $isImage = 'N'; } fputcsv($fp, array($product->getSku(), $isImage), ","); } if (count($products) < 50 || $p >= $products->getLastPageNumber()) { break; } } fclose($fp); |
In order to use this solution you have to create root script in Magento 2 and the output will display in the CSV format at the location where your root script is located.
The output of the above code looks like below:
|
|
---|---|
sku | With active image |
9789569532818 | Y |
9789569539034 | Y |
9789569535978 | Y |
9789569535984 | N |
9789569536542 | Y |
9789569539856 | Y |
9789569537548 | N |
9789569537875 | Y |
9789569537987 | N |
9789569538752 | Y |
You can check for all products in your store. If the product image does not exist, it represents as ‘N’ means ‘NO’ otherwise it shows ‘Y’ means ‘YES’ in the CSV file.
Your product image status list is ready!
If you have any doubt regarding the above solution, do mention them in the Comments section below.
I would be happy to help.
Feel free to share the method with Magento Community via social media.
Thank You.
Jignesh Parmar
An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.
Prev
Meetanshi Magento Extensions Launches and Updates February[2021]
How to Enable Cookie Restriction Mode in Magento 2
Next