Sunday, May 8

How to get Most Viewed products in Magento

Here We will see how to get most viewed Products in Magento

here is the code:

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;
}

Hope it Helps! Thanks..

No comments:

Post a Comment