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.
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(); ?>