How to Get Most Viewed Product Collection
Displaying the most viewed products is usually used to attract customers to the products and also works as a recommendation for them. This most viewed product collection is especially helpful for first-time-visit users who haven’t known much about products sold in the store. It helps them choose the best items more easily.
Most Viewed Products play an important role to help your store boost sales. This is because displaying these products on your site can guide customers to the collection of the most interesting products instead of letting them wander around without orientation. However, to display the most viewed products, you need to get the most viewed product collection. Therefore, in today’s article, I will guide you on how to get most viewed product collection in Magento 2.
3 Steps to get most viewed product collection
- Step 1: Create MostViewedProducts block
- Step 2: Insert in phtml file
- Step 3: Flush Cache & Test result
Step 1: Create MostViewedProducts block
To get most viewed product collection, firstly, you need to create a MostViewedProducts
block. To do that, follow the path Mageplaza/Productslider/Block/MostViewedProducts.php
and add the below code:
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.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 Mageplaza
* @package Mageplaza_Productslider
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/
namespace Mageplaza\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 Mageplaza\Productslider\Helper\Data;
use Mageplaza\Productslider\Model\ResourceModel\Report\Product\CollectionFactory as MostViewedCollectionFactory;
/**
* Class MostViewedProducts
* @package Mageplaza\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: Insert in phtml file
After having the colletion in the block, now you can follow this snippet to get product colletion from the block Mageplaza/HelloWorld/view/frontend/templates/list.phtml
.
Next, please insert the following code in the phtml file
<?php
$collection = $block->getProductCollection();
foreach ($collection as $_product) {
echo $product->getName() . ' - ' . $product->getProductUrl() . '<br />';
}
Step 3: Flush Cache & Test result
Finally, let’s flush cache and test result.
Conclusion
Above are three steps to help you get most viewed product collection in Magento 2. I hope after reading this, you will be able to display your collection easily.
If you have any questions or get stuck while following this tutorial, don’t hesitate to let me know.
Thank you for reading!
Enjoyed the tutorial? Spread it to your friends!