You can use this code in any .phtml or .php files of Magento
Get overall Most viewed products:
<h2>MOst Viewed Products</h2> <?php $productCount = 5; $storeId = Mage::app()->getStore()->getId(); $products_list = Mage::getResourceModel('reports/product_collection') ->addAttributeToSelect('*') ->setStoreId($storeId) ->addStoreFilter($storeId) ->addViewsCount() ->setPageSize($productCount); print"<pre>"; foreach ($products_list as $product){ print_r($product); } ?>
I create an object of the Mage::getResourceModel(‘reports/product_collection’) and filter out the data.
Get Most viewed products for current category:
<h2>MOst Viewed Products</h2> <?php $productCount = 5; $storeId = Mage::app()->getStore()->getId(); $products_list = Mage::getResourceModel('reports/product_collection') ->addAttributeToSelect('*') ->setStoreId($storeId) ->addStoreFilter($storeId) ->addViewsCount() ->addCategoryFilter(Mage::registry('current_category')) ->setPageSize($productCount); print"<pre>"; foreach ($products_list as $product){ print_r($product); } ?>
Get Most viewed products for last 15 days:
<h2>MOst Viewed Products</h2> <?php $today = time(); $startdate = $today - (60*60*24*15); $from = date("Y-m-d", $startdate); $to = date("Y-m-d", $today); $productCount = 5; $storeId = Mage::app()->getStore()->getId(); $products_list = Mage::getResourceModel('reports/product_collection') ->addAttributeToSelect('*') ->setStoreId($storeId) ->addStoreFilter($storeId) ->addViewsCount() ->addViewsCount($from, $to) ->setPageSize($productCount); print"<pre>"; foreach ($products_list as $product){ print_r($product); } ?>
2 Comments
bestlivetv · March 20, 2018 at 9:38 pm
Hii
I want to put your code here but doesn’t work. may you explain why?
Thanks
public/vendor/magento/module-catalog/view/frontend/templates/product/list.phtml
Sushant Vishwas · May 30, 2018 at 3:58 pm
Because of the above code will only work for Magento Version 1