Showing posts with label most. Show all posts
Showing posts with label most. Show all posts

Tuesday, September 13, 2011

How to get Most Viewed products in Magento?

Here is the code to get most viewed Products in Magento
    /**
     * 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);
  
    print_r($products);