GetGalleryImages returns NULL on the Magento homepage

We have expanded the slider of the main page for the client so that they can place products in this space.

As part of this, there are three image slots where we want to get the main image of the product, and then two images from the media gallery (ideally random, but not the end of the world, if by ID).

To get a better understanding, see the screenshot of what we have: -

featprodslider

We create an assembly for this module using the following: -

$featured_products = Mage::getModel('catalog/product') ->getCollection() ->addAttributeToSelect('*') ->AddAttributeToFilter('featured', array('eq' => 1)); 

Getting the main product image without problems, this works great with the following: -

 <img class="gallery" src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(225); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>" /> 

And it's simple enough to make all three image slots use this main image, as shown in the image above.

When we try to call getGalleryImages, it always returns NULL (example, for example): -

 <?php if (count($this->getGalleryImages()) > 0): ?> <?php foreach ($this->getGalleryImages() as $_image): ?> <img class="gallery" src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(100); ?>" width="100" height="100" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" /> <?php endforeach; ?> <?php endif; ?> 

Please, someone can advise the best approach to bring up gallery images on the main page. Is there something we can include in the assembly, or do we need to add an observer.

Thanks in advance.

+2
source share
2 answers

Finally managed to get this job ...

 <?php $_images = Mage::getModel('catalog/product')->load($product->getId())->getMediaGalleryImages(); ?> <?php if($_images){?> <?php $i=0; foreach($_images as $_image) if ($i++ < 5) { $i++; ?> <img class="gallery" src="<?php echo $this->helper('catalog/image')->init($product, 'thumbnail', $_image->getFile())->resize(255); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" /> <?php } ?> <?php } ?> 

We included an if in the foreach to ensure that we return a maximum of 3 of the product’s media gallery images.

The final result, compared with the original image, looks like this: -

featprodslider2

+2
source

It looks like you are calling getGalleryImages() directly on the block. Call it on the product object (for example, $product->getGalleryImages() instead of $this->getGalleryImages() ).

0
source

Source: https://habr.com/ru/post/900046/


All Articles