How do I find out if product has a product image?
Asked Answered
M

2

11

To get the image src in a template file the following code is used:

$this->helper('catalog/image')->init($_product, 'small_image')->resize(200,100);

But how can I find out if there is an image associated with the product or if the placeholder image will be used?

(Magento v. 1.4.2)

Magellan answered 19/2, 2011 at 8:43 Comment(0)
C
27

Placeholder is always used if product dosen't have image.

You can check those methods:

$product->getSmallImage();
$product->getThumbnail();
$product->getImage();

If product has image those moethod will return path.

Or you can check this method

$product->getMediaGalleryImages();

UPDATE 14.10.2011

no_selection is set when you check in BO > Product Edit Page > Images 'No image' chackbox

Chicken answered 19/2, 2011 at 10:1 Comment(2)
Nice, for some reason I got the string "no_selection" returned on some products where the place-holder image was used, so right now I'm using $has_real_image_set = ($_product->getSmallImage() != null && $_product->getSmallImage() != "no_selection");Magellan
the good thing about getimageurl() is it tests if the image actually exists, whereas getimage() just shows you the path. both usefull for diffierent things, though you could do php file exists...Carnauba
T
3

Best solution is:

if($_product->getImage() && $_product->getImage() != 'no_selection'){//do}
Tasse answered 13/9, 2015 at 8:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.