1

On the products grid in the category view I need to display additional image for some products. When I have a gallery of product images:

$images = $product->getMediaGalleryImages();
foreach ($images as $item)
{
    //...
}

how to retrieve an image by its role in the gallery? For example, I'd like to get the image which is marked with the Thumbnail role (or with some other custom role).

Is there any way to do that?

enter image description here enter image description here

2

You can get products image by Type by following Code :

  $baseImage  = $product->getImage();
  $smallImage = $product->getSmallImage();
  $thumbnail  = $product->getThumbnail();

OR You can get types of gallery images of a product by following code:

  $existingMediaGalleryEntries = $product->getMediaGalleryEntries();

  foreach ($existingMediaGalleryEntries as $entry) {
      $entry->getTypes();
  }

OR If you have custom media role then :

  $customIcon = $product->getResource()->getAttribute('custom_media_attribute')->getFrontend()->getValue($product);
|improve this answer|||||
  • And what if I have a custom role (when I created a custom attribute with Input Type Media Image)? How to get image of custom role? – zitix Mar 21 '18 at 13:18
  • @zitix See my updated answer. – Sujeet Pandit Mar 22 '18 at 5:13

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.