Get recently viewed products array in Magento

Get recently viewed products array in Magento

July 16, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Get recently viewed products array in Magento

Solving the Recently Viewed Products Array Issue in Magento

As an experienced e-commerce developer working with Magento, I often face customization challenges that require deep access to core functionality. One such issue is retrieving recently viewed products data for use in custom templates and layouts.

The Challenge: Accessing Recently Viewed Products Data

You’ve attempted to fetch the recently viewed products array using the following code:

$recentlyViewed = Mage::getBlockSingleton('report/product_view')->getRecentlyViewedProducts();

However, you are encountering an empty array as a result. This is because the report_product_view block doesn’t contain this information by default.

The Solution: Utilizing Magento’s Event System

Magento provides a robust event system that allows us to tap into key actions like product viewing. To access recently viewed products data, we need to observe the catalog_controller_product_view_after event and store the relevant data ourselves.

First, create a custom module with an observer for this event:

<config>
    <modules>
        <MyCompany_RecentlyViewedProducts>
            <version>1.0</version>
        </MyCompany_RecentlyViewedProducts>
    </modules>
    <events>
        <catalog_controller_product_view_after>
            <observers>
                <mycompany_recentlyviewed_products>
                    <type>singleton</type>
                    <class>MyCompany_RecentlyViewedProducts_Model_Observer</class>
                    <method>logProductViewedEvent</method>
                </mycompany_recentlyviewed_products>
            </observers>
        </catalog_controller_product_view_after>
    </events>
</config>

Next, implement the observer method to save the recently viewed product IDs:

public function logProductViewedEvent($observer)
{
    $session = Mage::getSingleton('core/session');
    $productId = $observer->getProduct()->getId();

    // Check if session variable exists and is not empty
    if (!$session->getRecentlyViewed()) {
        $recentlyViewedProducts = array();
    } else {
        $recentlyViewedProducts = unserialize($session->getRecentlyViewed());
    }

    // Add the viewed product to the session array, ensuring it doesn't exceed 10 items
    if (count($recentlyViewedProducts) < 10) {
        $recentlyViewedProducts[] = $productId;
    } else {
        array_shift($recentlyViewedProducts); // Remove oldest item
        $recentlyViewedProducts[] = $productId; // Add newest item
    }

    $session->setRecentlyViewed(serialize($recentlyViewedProducts));
}

This observer will automatically save the product IDs of recently viewed items in a session variable named RecentlyViewed.

Retrieving Recently Viewed Products Data

Now that you have the data saved, you can retrieve it in your custom template:

$session = Mage::getSingleton('core/session');
$recentlyViewedProducts = unserialize($session->getRecentlyViewed());

if ($recentlyViewedProducts) {
    // Process and display $recentlyViewedProducts array as needed
}

Conclusion: Customizing Magento’s Core Functionality

While Magento provides powerful built-in features, accessing certain data points requires a deep understanding of its event-driven architecture. By utilizing observers to capture key actions like product views, we can store and retrieve custom data with relative ease.

Remember that customizations should always be approached with caution, ensuring backward compatibility and minimal impact on core functionality. For more advanced Magento customization techniques, consider exploring Laravel Company’s Magento Development Services.

Let me know if you have any further questions about customizing Magento or need assistance with other e-commerce development tasks!

Catalog Extensions Themes Configuration

Our network