Change Price Decimal Separator for Arabic Store in Magento 2
Magento 2 stores are shopping platform for customers all over the world. Today we’ll talk about the price decimal separator in Magento 2 for Arabic store.
Normally when you show prices in Magento 2 stores, the integer part of the price gets separated using the dot. But when it comes to the Arabic store, it converts the price decimal separator from dot(.) to comma(,). Now due to the change of the separator, the value of the price gets changed. Let’s understand this using an example. Thousand is shown in English store as 1000.00. Switching to an Arabic store or changing the store view to Arabic changes the price decimal separator from dot to comma i.e 1000,00 which actually changes the value.
To solve this issue, today I have come up with the custom code to change price decimal separator for Arabic store in Magento 2. Use the steps below to serve the purpose.
Steps to Change Price Decimal Separator for Arabic Store in Magento 2
- First of all, create di.xml at app/code/Vendor/Extensionname/etc/di.xml and add below code into it.
1234<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"><preference for="Magento\Framework\Locale\Format" type="Vendor\Extensionname\Model\Format" /></config> - Now create model file at app/code/Vendor/Extensionname /Model/Format.php and put below code there.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859<?phpnamespace Vendor\Extensionname\Model;use Magento\Framework\Locale\Bundle\DataBundle;class Format extends \Magento\Framework\Locale\Format{private static $defaultNumberSet = 'latn';public function getPriceFormat($localeCode = null, $currencyCode = null){$localeCode = $localeCode ?: $this->_localeResolver->getLocale();if ($currencyCode) {$currency = $this->currencyFactory->create()->load($currencyCode);} else {$currency = $this->_scopeResolver->getScope()->getCurrentCurrency();}$localeData = (new DataBundle())->get($localeCode);$defaultSet = $localeData['NumberElements']['default'] ?: self::$defaultNumberSet;$format = $localeData['NumberElements'][$defaultSet]['patterns']['currencyFormat']?: ($localeData['NumberElements'][self::$defaultNumberSet]['patterns']['currencyFormat']?: explode(';', $localeData['NumberPatterns'][1])[0]);//your main changes are gone here.....$decimalSymbol = '.';$groupSymbol = ',';$pos = strpos($format, ';');if ($pos !== false) {$format = substr($format, 0, $pos);}$format = preg_replace("/[^0\#\.,]/", "", $format);$totalPrecision = 0;$decimalPoint = strpos($format, '.');if ($decimalPoint !== false) {$totalPrecision = strlen($format) - (strrpos($format, '.') + 1);} else {$decimalPoint = strlen($format);}$requiredPrecision = $totalPrecision;$t = substr($format, $decimalPoint);$pos = strpos($t, '#');if ($pos !== false) {$requiredPrecision = strlen($t) - $pos - $totalPrecision;}if (strrpos($format, ',') !== false) {$group = $decimalPoint - strrpos($format, ',') - 1;} else {$group = strrpos($format, '.');}$integerRequired = strpos($format, '.') - strpos($format, '0');$result = [//TODO: change interface'pattern' => $currency->getOutputFormat(),'precision' => $totalPrecision,'requiredPrecision' => $requiredPrecision,'decimalSymbol' => $decimalSymbol,'groupSymbol' => $groupSymbol,'groupLength' => $group,'integerRequired' => $integerRequired,];return $result;}}
Follow the steps in proper way and you’ve nailed the issue of price decimal separator of Arabic store in Magento 2 with ease.
Any queries or doubts are welcomed in the comments section below, I would be glad to solve your queries. And yeah, don’t forget to hit the 5 stars below!
Happy Coding!
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
13 Comments
not working for all pages like listing page,category, minicart sections . only on productdetail page.
Hello Shihab,
We’ve used the same solution and it worked on every pages.
Are you facing any error?
Thank You
Thanks for help.
hi, i use this
but it is only working for pdp page,
not working for all pages.
can u please suggest any solution
Hello,
The above solution works on the product page.
For the other pages, you’ll have to apply changes in its core file.
Follow this link for reference: https://magento.stackexchange.com/questions/98454/how-to-change-price-decimal-symbol-in-magento-2-complete-store
Thank You.
Where can I add these files in magento 2.3.4 , please advice
Hello,
You can add these files in a custom extension or else develop one to implement the above solution.
Thank you,
Hi Meetanshi,
I have added this code in my server. I have two store view.
1) English
2) Arabic
There are problem with arabic checkout page price format like this: 64٬83٫00 and product page is ok like this: 6483٫00.
Can you please tell me how to do price show like product page?
Thank you in advance.
Regards,
Manish
HI
I have add same path and code, But price is not getting chnaged
Please help
Hi Mamta,
Check if any extension in your store is overriding Magento\Framework\Locale\Format?
Try with putting a log in the extended file to debug further.
Hope this helps.
Thanks, its a really nice extension code.
There is small issue like price format change in product listing and detail page but not on cart and minicart page.
Can you please do needful like how to change arabic price format over there ?
Regards
Hi Gopal,
Using this code will automatically change the price format everywhere. If not, there may be an issue from your side.
thanks really help for me..