Magento 2 Get Best Sellers Collection
In an online store, each product will have a different number of sales. It’s necessary to catch up with the sales of all products in your stores to see which product is selling best and which one is not.
When it comes to best-selling products, it’s usually used as a good recommendation for customers, especially the first-comers. It’s also a wise strategy to display best-seller products in your stores to gain more sales in the peak of the shopping season.
To display the best seller products, you will need the data about best sellers collection. Therefore, in today post, I will provide you three stages to help you get bestseller collection in Magento 2.
How to get best seller collection in 3 steps
- Step 1: Create BestSellerProducts block
- Step 2: Insert in phtml file
- Step 3: Flush Cache & Test result
Step 1: Create BestSellerProducts block
To get a bestseller collection, the first thing you will have to do is create BestSellerProducts block. Follow this path Mageplaza/Productslider/Block/BestSellerProducts.php
<?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 Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory as BestSellersCollectionFactory;
use Mageplaza\Productslider\Helper\Data;
/**
* Class BestSellerProducts
* @package Mageplaza\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: Insert in phtml file
After having a collection in the block, now you can get product collection from the block by following the above snippet Mageplaza/HelloWorld/view/frontend/templates/list.phtml
.
Then, insert the below 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 is the instruction on how to get bestseller collection in Magento 2. I hope that after reading this tutorial, you will successfully apply three steps and manage the collection with ease.
If you find it helpful, don’t forget to share it with Magento friends to help them out.
Thanks for reading!
Enjoyed the tutorial? Spread it to your friends!