Skip to main content
eCommerce

How to Showcase Bestseller Products in Magento 2?

By |December 20, 2019 February 9th, 2022No Comments

Improve conversion rate in your eCommerce store by choosing Magento services from us and get your brand new Magento 2 store for your business. Let’s Talk

The ‘Bestseller Products’ section helps in finding products that are in the list of top-selling or frequently bought products. In other words, when a product is top-rated and has been sold in vast numbers, it is termed as a ‘Bestseller.’ In this blog, we will show you how to display the bestseller products in Magento 2.

Let’s get started by creating the bestseller product module, as shown in the file structure below.

Magento Bestseller Product Module

Step 1:

Create a block to get the bestseller product collection.

app/code/DCKAP/BestSellerProducts/Block/BestSellerProduct.php

_resourceFactory = $resourceFactory;
    	$this->product = $product;
    	$this->_imageHelper = $context->getImageHelper();
    	$this->_cartHelper = $context->getCartHelper();
    	parent::__construct($context, $data);
	}
 
	public function imageHelperObj(){
    	return $this->_imageHelper;
	}
	public function getProduct($id){
     	return $this->product->load($id);
	}
	/**
 	To get featured product collection
 	*/
	public function getBestsellerProduct(){
 	$resourceCollection = $this->_resourceFactory->create('MagentoSalesModelResourceModelReportBestsellersCollection');
 	$resourceCollection->setPageSize(10);
 	return $resourceCollection;
	}
   
	public function getAddToCartUrl($product, $additional = [])
	{
  	return $this->_cartHelper->getAddUrl($product, $additional);
	}
	 
	public function getProductPriceHtml(
    	MagentoCatalogModelProduct $product,
    	$priceType = null,
    	$renderZone = MagentoFrameworkPricingRender::ZONE_ITEM_LIST,
    	array $arguments = []
	) {
    	if (!isset($arguments['zone'])) {
        	$arguments['zone'] = $renderZone;
    	}
    	$arguments['zone'] = isset($arguments['zone'])
        	? $arguments['zone']
        	: $renderZone;
    	$arguments['price_id'] = isset($arguments['price_id'])
        	? $arguments['price_id']
        	: 'old-price-' . $product->getId() . '-' . $priceType;
    	$arguments['include_container'] = isset($arguments['include_container'])
        	? $arguments['include_container']
        	: true;
    	$arguments['display_minimal_price'] = isset($arguments['display_minimal_price'])
        	? $arguments['display_minimal_price']
        	: true;

    	$priceRender = $this->getLayout()->getBlock('product.price.render.default');

    	$price = '';
    	if ($priceRender) {
        	$price = $priceRender->render(
            	MagentoCatalogPricingPriceFinalPrice::PRICE_CODE,
            	$product,
            	$arguments
        	);
    	}
    	return $price;
	}
}

Step 2:

Create a view file.

app/code/DCKAP/BestSellerProducts/view/frontend/templates/bestsellerproducts.phtml

getBestsellerProduct();
?>
    1. getProduct($item->getProductId()); ?> ‘ : ‘
  1. ‘ ?>
    imageHelperObj()->init($_product, ‘product_page_image_small’) ->setImageFile($_product->getFile()) ->resize($this->getProductimagewidth(),$this->getProductimageheight()) ->getUrl(); ?>

    escapeHtml($_product->getName()) ?> getProductPriceHtml($_product); ?>

    isSaleable()): ?> getTypeInstance()->hasRequiredOptions($_product)): ?> helper(‘MagentoFrameworkDataHelperPostHelper’); $postData = $postDataHelper->getPostData($block->getAddToCartUrl($_product), [‘product’ => $_product->getId()]); ?> getIsSalable()): ?>

    ‘ : ” ?>

Step 3:

Create routes.xml app/code/DCKAP/BestSellerProducts/etc/frontend/routes.xml


	
    	
        	
    	
	

Step 4:

Create a layout to render the block and view file.app/code/DCKAP/BestSellerProducts/view/frontend/layout/bestseller_index_index.xml


	
    	
        	
    	
	

Step 5:

Create a controller to check the best seller grid.

app/code/DCKAP/BestSellerProducts/Controller/Index/Index.php

pageFactory=$pageFactory;
	}

	public function execute()
	{
    	return $this->pageFactory->create();
	}
}

Step 6:

Install the module.

Now, go to the browser: Type your Magento Store URL/Bestseller

Note: We can also call this block on the CMS page with our custom CSS.

{{block class="DCKAPBestSellerProductsBlockBestSellerProduct" 
template="DCKAP_BestSellerProducts::bestsellerproducts.phtml"}}

Bestseller products help in quickly identifying the top-rated products in an eCommerce store. You can now easily display the bestseller products using the Magento 2 module. We hope this blog was helpful to you. If you have any queries, please feel free to leave a comment below.

Vijayashanthi M

Author Vijayashanthi M

Vijayashanthi M is a passionate coder with an experience of 2.5 years in PHP, Yii 2, and Magento 2. She is always on the lookout for interesting things on the internet, and is an avid learner of new technologies. Her hobbies include dancing and listening to music.

More posts by Vijayashanthi M

Discover What You’re Missing

Get the weekly email full of actionable ideas and insights you can use at work and home.