Home » Magento26 April 2010

Magento: How to get most viewed products?

Here, I will show you the code to get the most viewed products in Magento. The function addViewsCount() filters the products with their views count.

Here is the code:-

Get overall Most viewed products

Get Most viewed products for current category

Get Most viewed products for last 30 days

Hope this helps. Thanks.

php magento mukesh chapagain

Get New Post by Email

Find me on

Facebook Twitter Google+ LinkedIn RSS Feed
  • http://nik.chankov.net Nik Chankov


    is there an easy way to get most viewed products for last 24 hours?


  • http://blog.chapagain.com.np Mukesh

    @Nik Chankov:-

    $today = time();
    $yesterday = $today - (60*60*24*1);

    $from = date("Y-m-d", $yesterday);
    $to = date("Y-m-d", $today);

    Now, add $from and $to to addViewsCount() function. Like this:-

    ->addViewsCount($from, $to)

    Now, you get most viewed products for last 24 hours.

    Hope this helps.

  • http://nik.chankov.net Nik Chankov

    Thanks, that’s what I needed :)

  • Mike

    Thanks Mukesh. Much appreciated.

  • http://www.hellokeykey.com/ hellokeykey

    I want to get a special product’s viewed. I am checking how to retrieve the data. please…
    best wish

  • tomas

    where I must put this sricpt ? pls help me ! :(

  • Yucef

    Could you please tell me how to modify the function to return the most viewed products in a specific category only?


  • Zeyedog

    Thanks man !!!
    You save my *ss and that’s not the first time
    Keep rollin’…

  • Anonymous

    thanks, working

  • http://www.facebook.com/fakedave Graham Brown

    Same here! Where does the code go?

  • http://blog.chapagain.com.np/ Mukesh

    See above for: Get Most viewed products for current category

    Here is the modified code to get products for specific category only:-

    $categoryId = 3; // PUT YOUR DESIRED CATEGORY ID
    $category = Mage::getModel(‘catalog/category’)->load($categoryId);

    $products = Mage::getResourceModel(‘reports/product_collection’)
    ->addCategoryFilter($category) // CHANGES DONE HERE

  • http://blog.chapagain.com.np/ Mukesh

    You can directly put the code or function in the template (.phtml) file where you want to display the most viewed products. But it’s always better to put these functions in class file of any module.

  • Ramamoorthy Govindaraj

    You write very good code, thanks!

  • disqus_Yx2FV7mAwb

    Hi, in which file do I add the code?

  • Manmeet

    Great post.

  • Elangovan

    Nice work, but please explain how to put this code in footer.phtml.when i put this code in footer.phtml, nothing to display.

    i want display popular product in footer.plz advice me.

  • deddyika

    it shows fatal error on a brand page which has more than one category, can you help me… the strange thing is, it is shows well on homepage (has many categories also)

  • Stuperfied

    Copy + Pasted your code then:

    gives me about 10 screens of keys and values. Im looking for 5 products at most.

  • rana ahamed

    bro where to write the code or where to place the code…….give a brief desc……

  • rana ahamed

    bro where to write the code or where to place the code…….give a brief desc……