One of our clients has a specific requirement to show some products(let's say 4 or 5) in the home page that are being viewed at the moment by other users. Your suggestions about how to begin with this would be greatly appreciated. Thanks.

2 Answers 2


Obviously you need a time limit for the viewed products.
Let's say you want to show the products viewed in the last 5 minutes.
You can modify the value to fit your own needs.
See my comments for each line:

//number of minutes to search viewed products
$minutes = 5;
//number of products to be shown
$limit = 5;
//instance of a date model to get the timestamps
/** @var Mage_Core_Model_Date $dateModel */
$dateModel = Mage::getModel('core/date');
//get the current date
$now = $dateModel->gmtDate();
//get the date X minutes ago
$pastDate = $dateModel->gmtDate(null, $dateModel->timestamp() - $minutes * 60);
//get the viewed products collection
/** @var Mage_Reports_Model_Resource_Product_Viewed_Collection $collection */
$collection = Mage::getResourceModel('reports/product_viewed_collection');
//set the time filter between now and X minutes ago
$collection->setDateRange($pastDate, $now);
//add minimum data to the product collection - prices, tax percents, and core attributes
//set a filter for the current website only - I know it says store it, but it actually filters by website
//in case you are showing this on a product page and you want to exclude the current product from the list uncomment the line below
//$collection->addAttributeToFilter('entity_id', array('neq'=>Mage::registry('current_product')->getId()));
//set the limit to Y products
$collection->setPage(1, $limit);
//do something with the product collection
foreach ($collection as $product) {
    echo $product->getId().'--'.$product->getName().'--'.$product->getProductUrl().'--'.$product->getFinalPrice().'<br />';

Wrap everything in one method and enjoy.


This might help (giving you the last 10 viewed products):

$collection = Mage::getModel('reports/product_index_viewed')

Your Answer

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

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