Question

We've extended a homepage slider for a client so that they can feature products in this space.

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

To get a better understanding, please see screenshot of what we have so far:-

featprodslider

We're building the collection for this module using the following:-

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

Getting the product's main image is no problem, this is working perfectly 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 is simple enough to make all three image slots use this main image as shown in the image above.

When we try to call getGalleryImages however, this always returns NULL (example such as):-

<?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 could someone advise of the best approach to call the gallery images on the homepage. Is there something we can include to the collection build or would we need to add an observer.

Thanks in advance.

Was it helpful?

Solution 3

We resolved this in the end and as @Marius points out, needed to be looking at getMediaGalleryImages.

The code we went with in the end is:-

<div class="images">
    <?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 } ?>
</div>

And here we load the product's media images and ensure we only show 3 for the 3 slots we have available in the product slider. Interested to know though if a better approach could have been achieved with your answer @Marius.

OTHER TIPS

I think the main question here is what type is $this in your context. It its a simple block it's kind of normal to return null if the method does not exist in the block.
On an other view....here is how you can get the images associated to a product assuming the variable $_product is an instance of the product itself:

$gallery = $_product->getMediaGalleryImages();

So in your case you might need something like this:

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

@zigojacko @Marius Recently I've come to a situation where I need to get all media gallery images from product collection without loading product model again as it will increase execution time and use extra memory. So I did R&D and found solution use below line before getMediaGalleryImages on the products you loaded in the collection.

$product->load('media_gallery');

Check code snippets below:

$products = Mage::getModel("catalog/product")->getCollection();
$galleryImagesLink = [];
$product->load('media_gallery');
$_images = $product->getMediaGalleryImages();
foreach ($_images as $image) {
    $galleryImagesLink[] = Mage::getModel('catalog/product_media_config')->getMediaUrl($image->getFile());
}

zigojacko asked for a better approach of his working code

Here it is: no need to load full product

<div class="images">
    <?php $_images = $product->load('media_gallery')->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 } ?>
</div>

Old question, but common problem... So maybe this helps other users

Even better, write your own method to get media_gallery values, so request will process kindly faster. Something like this...

protected $_resource = null;

public function getResource()
{
    if (is_null($this->_resource)) {
        $this->_resource = Mage::getSingleton('core/resource');
    }
    return $this->_resource;
}

protected function _getMediaPosition($valueId)
{
    $data = array();
    $readConnection = $this->getResource()->getConnection('core_read');
    $query = $readConnection->select()
        ->from($this->getResource()->getTableName('catalog_product_entity_media_gallery_value'))
        ->where('value_id = ?', $valueId);
    $results = $readConnection->fetchAll($query);
    if (! empty($results)) {
        $data = $results[0];
    }

    return $data;
}

public function getMediaGalleryImages($productId)
{
    $data = array();
    $gallery = array();

    $readConnection = $this->getResource()->getConnection('core_read');
    $query = $readConnection->select()
        ->from($this->getResource()->getTableName('catalog_product_entity_media_gallery'))
        ->where('entity_id = ?', $productId);
    $results = $readConnection->fetchAll($query);

    if (! empty($results)) {
        foreach ($results as $mediaGalleryRow) {
            $gallery[] = array_merge($mediaGalleryRow, $this->_getMediaPosition($mediaGalleryRow['value_id']));
        }
    }

    foreach ($gallery as $i => $element) {
        if ($element['disabled'] == 1) {
            continue;
        }

        $obj = new Varien_Object();
        $obj->setFile($element['value']);
        $data[$element['position'] . '_' . $i] = $obj;
    }

    ksort($data);
    return $data;
}

So, after adding that code in your block, template code would be

<div class="images">
    <?php $_images = $this->getMediaGalleryImages($product->getId()); ?>    
    <?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 } ?>
</div>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top