Model/ResourceModel/Catalog.php
public function getMostViewedProductCollection($from, $to, $limit, $catId, $catName)
    {
        $reportProductCollection = $this->reportProductCollection->create()
            ->addViewsCount($from, $to)
            ->setPageSize($limit);

        //filter collection by category by category_id
        if ($catId) {
            $category = $this->categoryFactory->create();
            $this->categoryResource->load($category, $catId);
            if ($category->getId()) {
                $reportProductCollection->getSelect()
                    ->joinLeft(
                        ['ccpi' => $this->getTable('catalog_category_product_index')],
                        'e.entity_id = ccpi.product_id',
                        ['category_id']
                    )
                    ->where('ccpi.category_id =?', $catId);
            } else {
                $this->helper->log(
                    'Most viewed. Category id ' . $catId
                    . ' is invalid. It does not exist.'
                );
            }
        }

        //filter collection by category by category_name
        if ($catName) {
            $category = $this->categoryFactory->create()
                ->loadByAttribute('name', $catName);
            if ($category->getId()) {
                $reportProductCollection->getSelect()
                    ->joinLeft(
                        ['ccpi' => $this->getTable('catalog_category_product_index')],
                        'e.entity_id  = ccpi.product_id',
                        ['category_id']
                    )
                    ->where('ccpi.category_id =?', $category->getId());
            } else {
                $this->helper->log(
                    'Most viewed. Category name ' . $catName
                    . ' is invalid. It does not exist.'
                );
            }
        }

        return $reportProductCollection;
    }
public function getMostViewedProductCollection($from, $to, $limit, $catId, $catName)
    {
        $reportProductCollection = $this->reportProductCollection->create()
            ->addViewsCount($from, $to)
            ->setPageSize($limit);

        //filter collection by category by category_id
        if ($catId) {
            $category = $this->categoryFactory->create();
            $this->categoryResource->load($category, $catId);
            if ($category->getId()) {
                $reportProductCollection->getSelect()
                    ->joinLeft(
                        ['ccpi' => $this->getTable('catalog_category_product_index')],
                        'e.entity_id = ccpi.product_id',
                        ['category_id']
                    )
                    ->where('ccpi.category_id =?', $catId);
            } else {
                $this->helper->log(
                    'Most viewed. Category id ' . $catId
                    . ' is invalid. It does not exist.'
                );
            }
        }

        //filter collection by category by category_name
        if ($catName) {
            $category = $this->categoryFactory->create()
                ->loadByAttribute('name', $catName);
            if ($category->getId()) {
                $reportProductCollection->getSelect()
                    ->joinLeft(
                        ['ccpi' => $this->getTable('catalog_category_product_index')],
                        'e.entity_id  = ccpi.product_id',
                        ['category_id']
                    )
                    ->where('ccpi.category_id =?', $category->getId());
            } else {
                $this->helper->log(
                    'Most viewed. Category name ' . $catName
                    . ' is invalid. It does not exist.'
                );
            }
        }

        return $reportProductCollection;
    }
public function getMostViewedProductCollection ( $ from , $ to , $ limit , $ catId , $ catName ) { $ reportProductCollection = $ this -> reportProductCollection -> create ( ) -> addViewsCount ( $ from , $ to ) -> setPageSize ( $ limit ) ; //filter collection by category by category_id if ( $ catId ) { $ category = $ this -> categoryFactory -> create ( ) ; $ this -> categoryResource -> load ( $ category , $ catId ) ; if ( $ category -> getId ( ) ) { $ reportProductCollection -> getSelect ( ) -> joinLeft ( [ 'ccpi' => $ this -> getTable ( 'catalog_category_product_index' ) ] , 'e.entity_id = ccpi.product_id' , [ 'category_id' ] ) -> where ( 'ccpi.category_id =?' , $ catId ) ; } else { $ this -> helper -> log ( 'Most viewed. Category id ' . $ catId . ' is invalid. It does not exist.' ) ; } } //filter collection by category by category_name if ( $ catName ) { $ category = $ this -> categoryFactory -> create ( ) -> loadByAttribute ( 'name' , $ catName ) ; if ( $ category -> getId ( ) ) { $ reportProductCollection -> getSelect ( ) -> joinLeft ( [ 'ccpi' => $ this -> getTable ( 'catalog_category_product_index' ) ] , 'e.entity_id  = ccpi.product_id' , [ 'category_id' ] ) -> where ( 'ccpi.category_id =?' , $ category -> getId ( ) ) ; } else { $ this -> helper -> log ( 'Most viewed. Category name ' . $ catName . ' is invalid. It does not exist.' ) ; } } return $ reportProductCollection ; }
Get most viewed product collection.

@param string $from
@param string $to
@param int $limit
@param int $catId
@param string $catName

@return \Magento\Reports\Model\ResourceModel\Product\Collection
Get most viewed product collection .