I have got the error below:

Error: Call to undefined method Magento\Catalog\Model\ResourceModel\Product\Collection\Interceptor::addViewsCount()


namespace Vendor\Mostviewed\Block;

class Mostviewed extends \Magento\Catalog\Block\Product\AbstractProduct
    protected $_filesystem ;
    protected $_imageFactory;

protected $_productCollectionFactory;

public function __construct(
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Framework\Filesystem $filesystem,         
    \Magento\Framework\Image\AdapterFactory $imageFactory,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    array $data = []
) {

    $this->_productCollectionFactory = $productCollectionFactory;
    parent::__construct($context, $data);

 * Get Product Collection of MostViewed Products
 * @return mixed
public function getMostViewProductCollection()
    $currentStoreId = $this->_storeManager->getStore()->getId();

    // get today and last 30 days time
    $today = time();
    $last = $today - (60*60*24*30);

    $from = date("Y-m-d", $last);
    $to = date("Y-m-d", $today);

    $collection = $this->_productCollectionFactory->create()

    $items = $collection->getItems();
    return $items;

// pass imagename, width and height
public function resize($image, $width = null, $height = null)
    $absolutePath = $this->_filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath('custom_module/posts/').$image;
    if (!file_exists($absolutePath)) return false;
    $imageResized = $this->_filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath('resized/'.$width.'/').$image;
    if (!file_exists($imageResized)) { // Only resize image if not already exists.
        //create image factory...
        $imageResize = $this->_imageFactory->create();         
        //destination folder                
        $destination = $imageResized ;    
        //save image      
    $resizedURL = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).'resized/'.$width.'/'.$image;
    return $resizedURL;


I want to get the most-view product collection. Please help me in this.


1 Answer 1


You are using the wrong class


You should use the below class


There is a difference between these two classes, the difference is module name changes to Reports

Hope it helps.

  • I have used same class which you provide. Got the same error. Aug 14, 2019 at 5:27
  • There is a difference between the two classes, the difference is an only module name changes to Reports. Share your whole file code to the question if possible
    – Mohit Rane
    Aug 14, 2019 at 5:38
  • Please check hole block code. Aug 14, 2019 at 5:49
  • change this class in your construct method \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory to \Magento\Reports\Model\ResourceModel\Product\CollectionFactory
    – Mohit Rane
    Aug 14, 2019 at 5:51
  • glad I could help
    – Mohit Rane
    Aug 14, 2019 at 5:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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