Search code examples
phpmysqlmagentoproductmage

Magento recently viewed products


What I have tried. Just get viewed products and display it, as result i got nothing:

 <?php $_recentlyViewed =
 Mage::getSingleton('Mage_Reports_Block_Product_Viewed')-> getItemsCollection(); ?>

     <?php foreach ($_recentlyViewed as $_recentlyProduct): ?>
         <?php var_dump($_recentlyProduct); ?>
     <?php endforeach; ?>

The next thing, it was trying to display the default block, then I got nothing. In local.xml I addded:

<?xml version="1.0"?>
<layout version="0.1.0">
    <catalog_product_view>
        <reference name="content">
            <reference name="product.info"> 
                <block type="reports/product_viewed" name="product.recently.viewed" as="product_recently_viewed" template="reports/product_viewed.phtml"/> 
                <block type="poll/activePoll" name="right.poll" after="product_recently_viewed">
                    <action method="setPollTemplate"><template>poll/active.phtml</template><type>poll</type></action>
                    <action method="setPollTemplate"><template>poll/result.phtml</template><type>results</type></action>
                </block>
           </reference>
    </reference>
</catalog_product_view>

And

<?php echo $this->getChildHtml('product_recently_viewed'); ?>

In view.phtml, but still I got nothing. I checked the table in mysql, there's records about last viewd products. So my question is, what made I wrong? Any ideas?


Solution

  • You have used getSingleton try using this:

     <?php $_recentlyViewed =
     Mage::getModel('Mage_Reports_Block_Product_Viewed')-> getItemsCollection(); ?>
    
     <?php foreach ($_recentlyViewed as $_recentlyProduct): ?>
         <?php var_dump($_recentlyProduct); ?>
     <?php endforeach; ?>
    

    By using getModel, I think your work can be achieved