I'am trying to get the image product url from getItemsCollection generated by Mage_Reports_Block_Product_Viewed. Here is my code. Everything is working except getImageUrl()

$nb = 3;
$already_used = array();
$collection = Mage::getSingleton('Mage_Reports_Block_Product_Viewed')->getItemsCollection();
foreach ($collection as $item){
    if ($nb > 0)
        <div class="product">
            <div class="image_product">
                <a href="<?php echo $item->getProductUrl(); ?>"><img src="<?php echo $item->getImageUrl(); ?>" alt="<?php echo $item->getName();?>" /></a>
            <div class="title_product">
                <h3><a href="<?php echo $item->getProductUrl(); ?>"><?php echo $item->getName(); ?></a></h3>
        $already_used[] = (int)$item->getProductId();

I don't understand what is wrong in my code...

Thank you for your help !


$item should be an instance of Mage_Catalog_Model_Product so you should be able to get the image url using the catalog image helper:

$imageUrl = (string)Mage::helper('catalog/image')->init($item, 'image');

So your code should look like this (only the section that should display the image)

<div class="image_product">
    <a href="<?php echo $item->getProductUrl(); ?>">
       <img src="<?php echo Mage::helper('catalog/image')->init($item, 'image')/*->resize(200, 200)*/; ?>" alt="<?php echo $item->getName();?>" />

If you want the image at a specific size, just uncomment the resize method and change the width and height.

| improve this answer | |
  • I understand but the product list I want is the last viewed product. Do you know how to adapt my current code for that purpose ? – axelparatre Jan 10 '14 at 8:02
  • @axelparatre. See my update. – Marius Jan 10 '14 at 8:05
  • I try it but it's not working – axelparatre Jan 10 '14 at 8:17

I find a solution based on that I found on the Internet.

Add this line after if($nb > 0) { :

$Products_one = Mage::getModel('catalog/product')->load($item->getProductId());

Than change the code bellow :

<div class="image_product">
                <a href="<?php echo $item->getProductUrl(); ?>"><img src="<?php echo $Products_one->getImageUrl(); ?>" alt="<?php echo $item->getName();?>" /></a>

Do you think it's an acceptable solution in term of performance ?

| improve this answer | |

I know that almost the same answer already exist but try instead of 'image' use 'small_image'

<?php echo $this->helper('catalog/image')->init($item, 'small_image')->resize(135) ?>

That code is used everywhere where is used Mage_Reports_Block_Product_Viewed.

| improve this answer | |

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.