Home » Magento

Magento: How to get most viewed products?

26 April 2010 4,531 views Popularity: 10% Share/Bookmark

email

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.

Related posts:

  1. Magento: Get Bestselling products by category and date time
  2. Magento: How to get top rated products?
  3. Magento: How to get / filter all products by attribute value?
  4. Magento: Get sub categories and product count
  5. Magento: Downloadable products not displayed in Associated Products tab in Grouped Product
  6. Magento: Load store specific product
  7. Magento 1.4: No products displayed in category listing
  8. Magento: How to get all associated children product of a configurable product?
  9. Displaying all products and new products listing in column/grid layout – Zen-cart
  10. Magento: Up-sells, Cross-sells and Related products are not showing up
  • 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