1

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>
            <div class="title_product">
                <h3><a href="<?php echo $item->getProductUrl(); ?>"><?php echo $item->getName(); ?></a></h3>
            </div>
        </div>
        <?php
        $already_used[] = (int)$item->getProductId();
    }
    $nb--;
}

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

Thank you for your help !

2

$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');

[EDIT]
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();?>" />
    </a>
</div>

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

  • 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
2

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>
            </div>

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

2

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.

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.