Consider the example code found in my own block file:
public function getImages()
{
$images = array();
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter(
'status',
array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
);
$products->getSelect()->order('rand()');
$products->getSelect()->limit(3);
foreach ($products as $product) {
$imageUrl = $product->getImageUrl();
$image['imageUrl'] = $imageUrl;
$image['title'] = $product->getName();
$images[] = $image;
}
return $images;
}
In my template file, I have some expected corresponding code:
<div class="images">
<?php foreach ($this->getImages() as $image): ?>
<img src="<?php echo $image['imageUrl'] ?>" title="<?php echo $image['title'] ?>">
<?php endforeach ?>
</div>
This completely works as expected. Three random products are chosen and an image is displayed for each (with the product name as title):
- Product 1 - Image1 - Title1
- Product 2 - Image2 - Title2
- Product 3 - Image3 - Title3
When I attempt to call the image resize helper, however, things get weird. Focus on the foreach ($products as $product)
section from the working piece of code above, and see the difference below:
foreach ($products as $product) {
// Use the resizer instead of directly calling $product->getImageUrl()
$imageUrl = $this->helper('catalog/image')->init($product, 'image')->resize(200)->keepFrame(false);
$image['imageUrl'] = $imageUrl;
$image['title'] = $product->getName();
$images[] = $image;
}
I would expect this to work properly, but instead each product is using the image for the last product listed, but the title remains correct:
- Product 1 - Image3 - Title1
- Product 2 - Image3 - Title2
- Product 3 - Image3 - Title3
It's clear that $image['imageUrl']
is getting assigned correctly in each iteration of the loop, because echo $image['imageUrl']
inside the loop displays the correct URL. Ultimately $image['imageUrl']
is getting reassigned for all iterations to the final value. But why does this occur when using the helper and it doesn't when calling the attribute value directly?
$this->helper('catalog/image')...
to a(string)
resolves the issue.