I have a loop in a phtml file that search product for a particular attribute. I am beginner on PHP for Magento and I managed to make this foreach.

What I need now is to make a loop that shows the most recently viewed products programmatically to show in my custom phtml file.

Like this ::: http://image.prntscr.com/image/8111de5ee8a94957bc7bd815f756040c.png

Anyone help me to make this loop?

$_productCollection = Mage::getModel('catalog/product')
    // add sorting to collection optional
    ->addAttributeToFilter('a_nao_pode_faltar_dtumpouco', array('eq' => 1))
    ->addAttributeToSort('created_at', 'DESC')

    foreach ($_productCollection as $_product){

This loop I made for custom product attribute! Now, I need a loop for lastviewed products.

1 Answer 1


Marcos Vinicius,magento default have this feature.So there are no need of doing any custom code.

  • The recent product's block class is Mage_Reports_Block_Product_Viewed and
  • template phtml file is app/design/frontend/Your_Package/Your_Theme/template/reports/product_viewed.phtml.

You need phtml using below code:

echo $this->getLayout()->createBlock('reports/product_viewed') ->setTemplate('reports/product_viewed.phtml')->toHtml();

Or you can do via layout report.xml at app/design/frontend/Your_Package/Your_Theme/layout and call thism block from layout

  • It's possible to edit product_viewed.phtml file to customize with my html/css? Jun 8, 2016 at 2:51
  • yes ,sure.it possible..you can do
    – Amit Bera
    Jun 8, 2016 at 2:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.