How do I get most viewed products in custom Magento2? I already found the workaround for top sellers but I do not get nothing about views
Magento 2 – How to Get Most Viewed Products
magento2reportsview
magento2reportsview
How do I get most viewed products in custom Magento2? I already found the workaround for top sellers but I do not get nothing about views
Best Answer
By default, Magento 2 shows the most viewed product in Magento Admin > DASHBOARD > choose tab Most Viewed Products. The
_prepareCollection()
method invendor/magento/module-backend/Block/Dashboard/Tab/Products/Viewed.php
is used for selecting products.We can copy some code lines from
_prepareCollection()
above. For example, in your block, use the following code below:The initial class
\Magento\Reports\Model\ResourceModel\Product\CollectionFactory
is the most important class because this class will get our products report. ThegetCollection
method will return the most viewed items by current store.In your template, you can get the items. For example: