Magento: Accessing models/blocks from a phtml

Magento: Accessing models/blocks from a phtml

October 1, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: Accessing models/blocks from a phtml

Magento: Accessing Recently Viewed Products in catalog/product/view.phtml

In Magento, accessing data from blocks like product_viewed within another template file can be a bit tricky if you’re not familiar with how Magento’s block system works. Let’s dive into how to access the recently viewed products in your catalog/product/view.phtml file.

Understanding Mage_Reports_Block_Product_Viewed

First, let’s clarify where the method $this->getRecentlyViewedProducts() originates from. The block Mage_Reports_Block_Product_Viewed indeed doesn’t have this method directly. Instead, it extends Mage_Reports_Block_Product_Abstract, which has a method _toHtml().

The key lies in the fact that Magento’s blocks often don’t define all their functionality; instead, they inherit from core or local blocks that provide the main logic.

Accessing Recently Viewed Products

To access recently viewed products from your catalog/product/view.phtml file, you can use the following approach:

<?php
// Get the block instance of Mage_Reports_Block_Product_Viewed
$reportsBlock = $this->getLayout()->createBlock('reports/report_product_viewed');

// Use the getRecentlyViewedProducts() method to fetch products
$_products = $reportsBlock->getRecentlyViewedProducts();

Let me break this down:

  1. getLayout(): This method retrieves the current layout object, which is an instance of Mage_Core_Model_Layout. It manages all the blocks in a page.

  2. createBlock('reports/report_product_viewed'): This line creates an instance of the ‘report_product_viewed’ block (which corresponds to the Mage_Reports_Block_Product_Viewed class). The format 'reports/report_product_viewed' is the standard way Magento refers to blocks using their module name and block alias.

  3. $reportsBlock->getRecentlyViewedProducts(): Now that we have an instance of the Mage_Reports_Block_Product_Viewed block, we can call its method directly.

Checking for Recently Viewed Products

If you just need to check if there are any recently viewed products without actually displaying them, you can simplify this:

<?php
$reportsBlock = $this->getLayout()->createBlock('reports/report_product_viewed');
$_hasProducts = !empty($_products);

Further Reading

For a deeper understanding of Magento’s block system and how to access blocks in templates, I recommend checking out this comprehensive guide on Magento Blocks from our blog at LaravelCompany.

Conclusion

In conclusion, accessing data from other blocks in Magento is possible by using the layout object’s createBlock() method. Remember to use the correct module name and block alias when creating a new block instance.

Always ensure you have a good understanding of your specific Mage_Reports_Block_Product_Viewed implementation, as some functionality might be custom or overridden in your theme or local code pool.

Happy coding! If you found this helpful, please share it with your Magento developer community.

Catalog Themes

Our network