Magento get popular products by total number of views

Posted by Damodar Bashyal on May 10, 2013

 

Magento product report resource collection allows you to get most popular products based on different options for example total number of product views. The code below will show you how to load magento's most viewed products collection.

functions we will use: addViewsCount, setOrder

Let's load this resource collection first like this:

$storeId = Mage::app()->getStore()->getId();
$products = Mage::getResourceModel('reports/product_collection')
     ->addAttributeToSelect('*')
     ->setStoreId($storeId)
     ->addStoreFilter($storeId)
     ->setPageSize(5);

Now, we can pass our filter on the product collection:

$products->addViewsCount();
// you can pass date range as well
$from = '2013-01-01';
$to = '2013-01-10';
$products->addViewsCount($from, $to);
$products->setOrder('orders', 'desc');

Source: app\code\core\Mage\Reports\Model\Resource\Product\Collection.php where addViewsCount looks like below in magento 1.7.0.2

    /**
     * Add views count
     *
     * @param string $from
     * @param string $to
     * @return Mage_Reports_Model_Resource_Product_Collection
     */
    public function addViewsCount($from = '', $to = '')
    {
        /**
         * Getting event type id for catalog_product_view event
         */
        foreach (Mage::getModel('reports/event_type')->getCollection() as $eventType) {
            if ($eventType->getEventName() == 'catalog_product_view') {
                $productViewEvent = (int)$eventType->getId();
                break;
            }
        }

        $this->getSelect()->reset()
            ->from(
                array('report_table_views' => $this->getTable('reports/event')),
                array('views' => 'COUNT(report_table_views.event_id)'))
            ->join(array('e' => $this->getProductEntityTableName()),
                $this->getConnection()->quoteInto(
                    "e.entity_id = report_table_views.object_id AND e.entity_type_id = ?",
                    $this->getProductEntityTypeId()))
            ->where('report_table_views.event_type_id = ?', $productViewEvent)
            ->group('e.entity_id')
            ->order('views ' . self::SORT_ORDER_DESC)
            ->having('COUNT(report_table_views.event_id) > ?', 0);

        if ($from != '' && $to != '') {
            $this->getSelect()
                ->where('logged_at >= ?', $from)
                ->where('logged_at <= ?', $to);
        }

        $this->_useAnalyticFunction = true;
        return $this;
    }

And, setOrder looks like this:

 /**
     * Set order
     *
     * @param string $attribute
     * @param string $dir
     * @return Mage_Reports_Model_Resource_Product_Collection
     */
    public function setOrder($attribute, $dir = self::SORT_ORDER_DESC)
    {
        if (in_array($attribute, array('carts', 'orders','ordered_qty'))) {
            $this->getSelect()->order($attribute . ' ' . $dir);
        } else {
            parent::setOrder($attribute, $dir);
        }
 
        return $this;
    }

Similarly there are other options too:

Noel posted on - Tuesday 15th of April 2014 08:16:15 AM

Hi, can you help me on how to display this on a category list page sir?

thanks!
 
not published on website


QR Code: Magento get popular products by total number of views