Ecommerce Magento 2 Magento 2 Tutorials Magento Development

Magento 2: How to Get Most Viewed Product Collection?

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...Loading...
Get Most Viewed Product Collection Magento 2

Today, we’re going to teach you guys how to get the most viewed product collection in Magento 2 store.

Identifying the most viewed products play a crucial role in boosting sales of your Magento 2 store. This is mainly because the most viewed products in your store can help you to understand the demand of your customers and improve sales.

And in this post, we’ve shared a complete, step-by-step tutorial on how to get the most viewed products collection in your Magento 2 store.

Step-By-Step Process to Get Most Viewed Product Collection in Magento 2

Please follow the below steps to learn how to get most viewed product collection in your Magento 2 store.

Step – 1

First of all, you need to create a MostViewedProducts.php file in the MageDelight/Productslider/Block/ folder & paste the following code to get the most viewed product collection in the block.

<?php

/**

 * MageDelight

 *

 * NOTICE OF LICENSE

 *

 * This source file is subject to the MageDelight.com license that is

 * available through the world-wide-web at this URL:

 * https://www.MageDelight.com/LICENSE.txt

 *

 * DISCLAIMER

 *

 * Do not edit or add to this file if you wish to upgrade this extension to newer

 * version in the future.

 *

 * @category    MageDelight

 * @package     MageDelight_Productslider

 * @copyright   Copyright (c) MageDelight (https://www.MageDelight.com/)

 * @license     https://www.MageDelight.com/LICENSE.txt

 */

namespace MageDelight\Productslider\Block;

use Magento\Catalog\Block\Product\Context;

use Magento\Catalog\Model\Product\Visibility;

use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;

use Magento\Framework\App\Http\Context as HttpContext;

use Magento\Framework\Stdlib\DateTime\DateTime;

use MageDelight\Productslider\Helper\Data;

use MageDelight\Productslider\Model\ResourceModel\Report\Product\CollectionFactory as MostViewedCollectionFactory;

/**

 * Class MostViewedProducts

 * @package MageDelight\Productslider\Block

 */

class MostViewedProducts extends AbstractSlider

{

    /**

     * @var MostViewedCollectionFactory

     */

    protected $_mostViewedProductsFactory;

    /**

     * MostViewedProducts constructor.

     * @param Context $context

     * @param CollectionFactory $productCollectionFactory

     * @param Visibility $catalogProductVisibility

     * @param DateTime $dateTime

     * @param Data $helperData

     * @param HttpContext $httpContext

     * @param MostViewedCollectionFactory $mostViewedProductsFactory

     * @param array $data

     */

    public function __construct(

        Context $context,

        CollectionFactory $productCollectionFactory,

        Visibility $catalogProductVisibility,

        DateTime $dateTime,

        Data $helperData,

        HttpContext $httpContext,

        MostViewedCollectionFactory $mostViewedProductsFactory,

        array $data = []

    ) {

        $this->_mostViewedProductsFactory = $mostViewedProductsFactory;

        parent::__construct($context, $productCollectionFactory, $catalogProductVisibility, $dateTime, $helperData, $httpContext, $data);

    }

    /**

     * Get Product Collection of MostViewed Products

     * @return mixed

     */

    public function getProductCollection()

    {

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

            ->addAttributeToSelect('*')

            ->setStoreId($this->getStoreId())->addViewsCount()

            ->addStoreFilter($this->getStoreId())

            ->setPageSize($this->getProductsCount());

        return $collection;

    }

}

Step – 2

After that, you need to create a list.phtml file in the MageDelight/HelloWorld/view/frontend/templates/ folder & paste the following code to get the product collection from the above block.

<?php

$collection = $block->getProductCollection();

foreach ($collection as $_product) {

    echo $product->getName() . ' - ' . $product->getProductUrl() . '<br />';

}

Step – 3

Finally, flush the cache and check the result.

Conclusion

And there you have it!

This is the easiest guide to get most viewed product collection in your Magento 2 store.

We hope that you found this tutorial helpful. If you have any doubts, please ask them in the comments below.

And if you need our professional assistance, feel free to contact us anytime.

You may also like
Social Media Management Tools for E-commerce
Top 5 Social Media Management Tools for E-commerce Businesses
Auto Filter Magento 2
How to Auto Select First Child Product of a Configurable Product in Magento 2?

Leave Your Comment

Your Comment*

Your Name*