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

public function getMostViewedProducts()
{    	
	/**
	 * Number of products to display
	 * You may change it to your desired value
	 */
	$productCount = 5; 
	
	/**
	 * Get Store ID
	 */
	$storeId    = Mage::app()->getStore()->getId();       
	
	/**
	 * Get most viewed product collection
	 */
	$products = Mage::getResourceModel('reports/product_collection')
		->addAttributeToSelect('*')		
		->setStoreId($storeId)
		->addStoreFilter($storeId)
		->addViewsCount()
		->setPageSize($productCount); 
	
	Mage::getSingleton('catalog/product_status')
			->addVisibleFilterToCollection($products);
	Mage::getSingleton('catalog/product_visibility')
			->addVisibleInCatalogFilterToCollection($products);
	
	return $products; 
}

Get Most viewed products for current category

public function getMostViewedProducts()
{ 	
	// number of products to display
	$productCount = 5; 
	
	// store ID
	$storeId    = Mage::app()->getStore()->getId();       
	
	// get most viewed products for current category
	$products = Mage::getResourceModel('reports/product_collection')
		->addAttributeToSelect('*')		
		->setStoreId($storeId)
		->addStoreFilter($storeId)
		->addViewsCount()
		->addCategoryFilter(Mage::registry('current_category'))
		->setPageSize($productCount); 
	
	Mage::getSingleton('catalog/product_status')
			->addVisibleFilterToCollection($products);
	Mage::getSingleton('catalog/product_visibility')
			->addVisibleInCatalogFilterToCollection($products);
	
	return $products; 
}

Get Most viewed products for last 30 days

public function getMostViewedProducts()
{ 	
	// number of products to display
	$productCount = 5; 
	
	// store ID
	$storeId    = Mage::app()->getStore()->getId(); 
	
	// get today and last 30 days time
	$today = time();
	$last = $today - (60*60*24*30);

	$from = date("Y-m-d", $last);
	$to = date("Y-m-d", $today);
	
	// get most viewed products for last 30 days
	$products = Mage::getResourceModel('reports/product_collection')
		->addAttributeToSelect('*')		
		->setStoreId($storeId)
		->addStoreFilter($storeId)
		->addViewsCount()
		->addViewsCount($from, $to)
		->setPageSize($productCount); 
	
	Mage::getSingleton('catalog/product_status')
			->addVisibleFilterToCollection($products);
	Mage::getSingleton('catalog/product_visibility')
			->addVisibleInCatalogFilterToCollection($products);
	
	return $products; 
}

Hope this helps and thanks for reading.

From Mukesh Chapagain's Blog, post Magento: How to get most viewed products?

php magento mukesh chapagain

Get New Post by Email

Find me on

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

    Hi,

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

    Cheers!

  • 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?

    Thanks!

  • 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’)
    ->addAttributeToSelect(‘*’)
    ->setStoreId($storeId)
    ->addStoreFilter($storeId)
    ->addViewsCount()
    ->addCategoryFilter($category) // CHANGES DONE HERE
    ->setPageSize($productCount);

  • 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)
    thanks

  • Stuperfied

    Copy + Pasted your code then:
    var_dump(getMostViewedProducts());

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