Hi everybody, I am Cuong, Welcome back to my Magento 2 tutorial video series. #magento2tutorial #magento2 #magento #magento2training #productonsale
In the previous lesson, I showed everybody, How to display the percentage of a discount per each product on the product list in Magento 2. You can watch again this lesson in the video link
Today, I am going to show everybody the best practice, How to display all products on sale in Magento 2.
What is the mean of “on sale”?
When a product can be bought for less than their regular price. So that product is on sale.
How can we get these products in Magento 2 by programmatically? This practice will show everybody the best solution and code.
By default, Magento 2 provides us a Widget Type named Catalog Products List, it allows to display the list of products in a grid, you can see this widget added to the block identifier named home-page-block after installing sample data successful. However, this widget doesn’t allow to filter the products on sale for displaying.
Follow this widget, we can add some additional codes allowing only displaying the products on sale.
This code helps us to get only the products having the final price less than the regular price, however, it can not get the configurable products on sale because of these products can’t be set the special price. The special price only is set to the variable products of it.
This is a configurable product. This codes can’t help to get the configurable products on sale.
So what is the solution? How can we get both simple and configurable products on sale for displaying?
We will create a new product attribute called on_sale, this attribute will allow determining a product is on sale or not. Before saving a product in the backend, if it isn’t a configurable product, we will check the special price of that product. If it is a configurable product, we will check the special price of all simple products of it. Then set the on_sale to “Yes” or “No”
So what will we do in this practice?
1. Create a new customization module named PHPCuong_ProductOnSale
2. Create a new product attribute named on_sale
3. Create a new widget named “Catalog Products On Sale List” for displaying the listing products on sale
Okie, let’s go.
Let’s complete this practice you need to follow steps by step:
Step 1: Declaring the new module called PHPCuong_ProductOnSale
Step 2: Adding the new product attribute named on_sale
Step 3: Set the value to the attribute named on_sale while saving the product.
– Before saving the product from the backend, we will check that product and set the value to the product attribute named on_sale. We will use the event named catalog_product_save_before for completing this step.
– After saving successful, we will check that product, if it isn’t a configurable product then get the Parent Products and set the value to the product attribute named on_sale.
Step 4: Declaring the new command-line interface (Magento CLI) named catalog:product:attribute:on_sale
– We need to create a new Magento CLI, It helps us to check all products, if these products are on sale then set the product attribute named on_sale of those products to “2”, else set it to “1”.
– Create the new file named di.xml in the path appcodePHPCuongProductOnSaleetc
– Create the new file named SetOnSaleValue.php in the path appcodePHPCuongProductOnSaleConsoleCommand
– We need to set cron job for updating the product attribute on_sale once a day. By running the command line
php bin/magento catalog:product:attribute:on_sale
Step 5: Declaring the new widget named Catalog Products On Sale List
– This widget will use the same template named grid.phtml with the module named Magento_CatalogWidget
– Create the new file named widget.xml in the path appcodePHPCuongProductOnSaleetc
– Create the new file named ProductsList.php in the path appcodePHPCuongProductOnSaleBlockProduct
Step 6: Test and see the results
1. Run the following command lines:
php bin/magento setup:upgrade –keep-generated
php bin/magento setup:di:compile
2. Go to the backend, then set the new widget to the homepage block try to save some products for adding the value to the product attribute on_sale
3. Go to the storefront.
4. Run the command line: php bin/magento catalog:product:attribute:on_sale
Yeah, it works perfectly
Thank you for watching guide. 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 on the Github