How to display best seller products on the homepage in Magento 2
Hi everybody, I am Cuong, Welcome back to my Magento 2 tutorial video series. #magento2tutorial #magento2training #magento2
Today, I am going to show everybody the best practice, How to display bestsellers products on the homepage in Magento 2.
By default Magento 2 allows us to show the products bestsellers report in the backend, you can see it by going to Admin Panel → Reports → Products → Bestsellers. Also, you can see the top 5 of bestsellers products in the tab named Bestsellers on the Dashboard. The data of this products bestsellers report is saved in the tables named:
To refresh bestsellers report, you go to Admin Panel → Reports → Statistics → Refresh Statistics, then submit the action named Refresh Lifetime Statistics.
However, Magento 2 doesn’t provide us the feature that allowing to show these bestsellers products on the homepage of the storefront. So in this practice, I’m going to show everybody the best codes to display correct the bestsellers products on the homepage with some simple steps.
1. I will create a new widget called “Bestsellers Products”
2. Displaying this widget on the homepage.
Okie, let’s go.
Let’s do this practice, you need to follow steps by step:
Step 1: Declaring the new module called PHPCuong_BestsellersProducts
– Create the namespace PHPCuong in the path appcode
– Create the module named BestsellersProducts in the path appcodePHPCuong
– Create the file named registration.php in the path appcodePHPCuongBestsellersProducts
– Create the file named module.xml in the path appcodePHPCuongBestsellersProductsetc
End of step #1, I have been completed the codes to create a new module called PHPCuong_BestsellersProducts.
Step 2: Declaring the new widget named Bestsellers Products
– Create the new file named widget.xml in the path appcodePHPCuongBestsellersProductsetc
– Create the new file named BestsellersProducts.php in the path appcodePHPCuongBestsellersProductsBlockWidget
– Create the new file named Collection.php in the path appcodePHPCuongBestsellersProductsModelResourceModelProduct
– Create the new file named bestsellers-products.phtml in the path appcodePHPCuongBestsellersProductsviewfrontendtemplateswidget
The content of the file named bestsellers-products.phtml, I copied from the file grid.phtml in the module named Magento_CatalogWidget
Step 3: Test and see the results
1. Run the following command lines:
php bin/magento setup:upgrade –keep-generated
php bin/magento cache:flush
2. Display the bestsellers products widget on the homepage.
Go to the Magento Admin Panel → Content → Elements → Widgets → Add Widget
– Add the new widget named Bestsellers Products
3. Test the results
Go to the homepage.
Yeah, it works perfectly.
Thank you for your watching. If you have any questions about this practice, please feel free to leave a comment below.
Don’t forget to like, comment, share and subscribe to my channel for getting the latest videos.
Please do not hesitate to contact me if you need me to join your Magento project. My rate is $25/hour in Magento 1 and $30/hour in Magento 2.
Get the source codes of this practice in the Github