Today, we’re going to teach you guys how to showcase best seller products in your Magento 2 store.
In any online store, every product has a different number of sales depending on its popularity and market demand.
So, for any online store owner, it’s highly recommended to identify products with the highest sales and suggest them to the new customers who have not bought them just yet.
The best way to go about this is by displaying them as best-seller products using a highlighted section on your Magento 2 store.
And in this post, we will show you exactly how to get the best seller products collection and display them in your Magento 2 store.
Steps to Showcase Best-Seller Products in Magento 2
Please follow the below steps to learn how to showcase best-seller products in your Magento 2 store.
Step - 1
First of all, we’ll need to get the bestseller products collection in a block.
For this, we need to create a block named BestSellerProducts.php in the MageDelight/Productslider/Block/ folder and paste the following code:
<?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 Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory as BestSellersCollectionFactory; use MageDelight\Productslider\Helper\Data; /** * Class BestSellerProducts * @package MageDelight\Productslider\Block */ class BestSellerProducts extends AbstractSlider { /** * @var BestSellersCollectionFactory */ protected $_bestSellersCollectionFactory; /** * BestSellerProducts constructor. * @param Context $context * @param CollectionFactory $productCollectionFactory * @param Visibility $catalogProductVisibility * @param DateTime $dateTime * @param Data $helperData * @param HttpContext $httpContext * @param BestSellersCollectionFactory $bestSellersCollectionFactory * @param array $data */ public function __construct( Context $context, CollectionFactory $productCollectionFactory, Visibility $catalogProductVisibility, DateTime $dateTime, Data $helperData, HttpContext $httpContext, BestSellersCollectionFactory $bestSellersCollectionFactory, array $data = [] ) { $this->_bestSellersCollectionFactory = $bestSellersCollectionFactory; parent::__construct($context, $productCollectionFactory, $catalogProductVisibility, $dateTime, $helperData, $httpContext, $data); } /** * get collection of best-seller products * @return mixed */ public function getProductCollection() { $productIds = []; $bestSellers = $this->_bestSellersCollectionFactory->create() ->setPeriod('month'); foreach ($bestSellers as $product) { $productIds[] = $product->getProductId(); } $collection = $this->_productCollectionFactory->create()->addIdFilter($productIds); $collection->addMinimalPrice() ->addFinalPrice() ->addTaxPercents() ->addAttributeToSelect('*') ->addStoreFilter($this->getStoreId())->setPageSize($this->getProductsCount()); return $collection; } }
Step - 2
After that, we need to insert the bestseller products collection in a phtml file from the block.
For this, create a list.phtml file in the MageDelight/HelloWorld/view/frontend/templates/ folder and paste the following code:
<?php $collection = $block->getProductCollection(); foreach ($collection as $_product) { echo $product->getName() . ' - ' . $product->getProductUrl() . '<br />'; }
Step - 3
Lastly, flush the cache and check the results.
Also read: How to Create a Downloadable Product in Magento 2 Programmatically?
Conclusion
And that’s about it!
This is the quickest way to showcase best seller products in a Magento 2 store.
And if you need our professional assistance with Magento 2 Development, feel free to reach out to us at any time.