How to remove the decimal from the price in Magento 2
Hi everybody, I am Cuong, Welcome back to my Magento 2 tutorial video series.
#Magento2Tutorial #RemoveDecimal #DecimalPrice
Today, I am going to show everybody the best practice, How to remove the decimal from the price in Magento 2.
By default, Magento 2 is showing the price with 2 decimals on the storefront, this is good for some currencies such as USD, EUR. But it is not good for the currency such as VND (Vietnam Dong). When you work for a project, it is required to remove the decimal from the price, you don’t know how to remove it, today, I show you the best codes to complete your task.
We have the scenario: We will provide a feature, that allows the administrator sets up showing the decimal with precision between 1 to 3 or remove the decimal from the price.
So what will we do in this practice?
1. We will create a new module named PHPCuong_PriceDecimal
2. We will use the Plugin to change the decimal precision or remove the decimal from the price
Before doing this practice, you need to know, How to use the Plugin in Magento 2, you can learn it in the video link
Okie, let’s go.
Let’s do this practice, you need to follow steps by step:
Step 1: Declaring the new module called PHPCuong_PriceDecimal
– Create the namespace PHPCuong in the path appcode
– Create the module named PriceDecimal in the path appcodePHPCuong
– Create the file named registration.php in the path appcodePHPCuongPriceDecimal
– Create the file named module.xml in the path appcodePHPCuongPriceDecimaletc
End of step #1, I have been completed the codes to create the new module called PHPCuong_PriceDecimal.
Step 2: Declaring the Configuration in the adminhtml
– We will define the new section called “Decimal Price” in the tab named Catalog.
– Create the new file named system.xml in the path appcodePHPCuongPriceDecimaletcadminhtml
– We will define the default value for these fields.
– Create the new file named config.xml in the path appcodePHPCuongPriceDecimaletc
Step 3: Using the Plugin to change the decimal precision or remove the decimal from the price
– Because we only remove the decimal from the price on the storefront, so the codes in this practice only apply to the storefront.
– Create the new file named di.xml in the path appcodePHPCuongPriceDecimaletcfrontend
– Create the new file named Data.php in the path appcodePHPCuongPriceDecimalHelper
– Create the new file named PriceCurrency.php in the path appcodePHPCuongPriceDecimalPluginFrameworkPricing
– Create the new file named Format.php in the path appcodePHPCuongPriceDecimalPluginFrameworkPricingLocal
– Create the new file named Order.php in the path appcodePHPCuongPriceDecimalPluginSalesModel
There is no method called “formatPricePrecision” in the class MagentoSalesApiDataOrderInterface, however, this class is preferenced by the class MagentoSalesModelOrder, you can see it here
– Create the new file named Currency.php in the path appcodePHPCuongPriceDecimalPluginFrameworkPricing
Okie, we completed the files code for this practice.
Step 4: Test and see the results
1. Run the following command lines:
php bin/magento setup:upgrade –keep-generated
You can see the new module is installed in the table named setup_module
php bin/magento cache:flush
2. Test the results
Go to the Magento Admin Panel → Stores → Settings → Configuration → Catalog → Decimal Price
We set the default value for these fields.
Go to the storefront. Oh, it still shows 2 decimals in the price, try to remove the decimal, it works perfectly when we remove the decimal. Yeah, it works perfectly on other places excepting the product detail page. I think this extension can work perfectly in other versions of Magento 2.2.x
In this practice I used Magento 2.1.13, you can try to do it in other versions of Magento 2.
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 my videos 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