1

how to get the most viewed product collection in your Magento 2 store?

I try with multiple codes but unfortunately, I am not getting any solution.

I try to create a MostViewedProducts.php file in the /Productslider/Block/ folder & paste the code to get the most viewed product collection in the block. but, not working.

1
  • share the code what you are doing and what not working. Commented Oct 14, 2021 at 11:44

2 Answers 2

2
<?php

namespace Vendor\Module\Block;


use Magento\Framework\View\Element\Template;

class Blockname extends Template
{
   protected $_productsFactory;
   protected $_storeManager;    

   public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Reports\Model\ResourceModel\Product\CollectionFactory $productsFactory,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    array $data = []
) {
    $this->_productsFactory = $productsFactory;
    $this->_storeManager = $storeManager;
    parent::__construct($context, $data);
}

public function getProductCollection()
{
    $currentStoreId = $this->_storeManager->getStore()->getId();

    $collection = $this->_productsFactory->create()
                       ->addAttributeToSelect('*')
                       ->addViewsCount()
                       ->setStoreId($currentStoreId)
                       ->addStoreFilter($currentStoreId);

    return $collection->getItems();
}
}



<?php
$collection = $block->getProductCollection();
foreach ($collection as $_product) {
echo $product->getName() . ' - ' . $product->getProductUrl() . '<br />';
}

Add above colllection in the phtml file assigned to block.

1

1.Create one block file on our custom extension add MostViewedProducts.php in following path app\code\Vendor\Extension\Block\MostViewedProducts.php

<?php

namespace Vendor\Extension\Block;


use Magento\Framework\View\Element\Template;

class MostViewedProducts extends Template
{
    protected $_productsFactory;
    protected $_storeManager;   

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Reports\Model\ResourceModel\Product\CollectionFactory $productsFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        array $data = []
    ) {
        $this->_productsFactory = $productsFactory;
        $this->_storeManager = $storeManager;
        parent::__construct($context, $data);
    }
   
    public function getProductCollection()
    {
        $currentStoreId = $this->_storeManager->getStore()->getId();

        $collection = $this->_productsFactory->create()
                           ->addAttributeToSelect('*')
                           ->addViewsCount()
                           ->setStoreId($currentStoreId)
                           ->addStoreFilter($currentStoreId);

        return $collection->getItems();
    }
}

2.Then create list.phtml file on following path app\code\Vendor\Extension\view\frontend\templates\list.phtml.

<?php
$collection = $block->getProductCollection();
foreach ($collection as $_product) {
    echo $product->getName() . ' - ' . $product->getProductUrl() . '<br />';
}

3.Then flush cache and test result.

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.