How to get bestseller and most viewed product in homepage Magento 2?

We have to display bestseller and most viewed product list in homepage slider in magento 2.

2 Answers 2


For bestseller create a block in __construct get instance of

\Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory $collectionFactory,


namespace Sugarcode\Test\Block;

class Test extends \Magento\Framework\View\Element\Template
    protected $_coreRegistry = null;
    protected $_collectionFactory;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
       \Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory $collectionFactory,
        array $data = []
    ) {
        $this->_collectionFactory = $collectionFactory;
        $this->_coreRegistry = $registry;
        parent::__construct($context, $data);

    public function _prepareLayout()
        return parent::_prepareLayout();
    public function getBestSellerData()
        $collection = $this->_collectionFactory->create()->setModel(

        return $collection;


For recently viewed you can use widget from admin side or else you can write custom block with \Magento\Reports\Model\ResourceModel\Product\CollectionFactory $productsFactory

Look at:



  • How to get list of most viewed products? Dec 1, 2015 at 6:30
  • create widget from admin , in front end call that widget, if bestsellers works for you then accept my answer so it will usefull for others Dec 1, 2015 at 6:31
  • ok, i will check it first for bestseller. Thanks. Dec 1, 2015 at 6:42
  • its working fine you will get product id and name, just put for each loop for the collection and print the data Dec 1, 2015 at 9:06
  • 1
    @RBJesadiya:- in forum you will not get 100% solution for your task , you will get only ideas , if you want raise oen more new ticket , you can try this $collection->getSelect()->limit(10); Dec 4, 2015 at 9:59

Use the following code to view the BEST SELLER as well as MOST VIEWED products in your Magento 2 Slider.

<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance();   
$productCollection = $objectManager->create('Magento\Reports\Model\ResourceModel\Report\Collection\Factory'); 
$collection = $productCollection->create('Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection'); ?>
  • Keep in mind when you first launch a site, you won't have any sales data, so you'll need to add backup logic, e.g., retrieve random products from catalog if there is nothing in sales table.
    – thdoan
    Mar 8, 2017 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.