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.
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.
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.
<?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.
<?php namespace 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.
Happy Coding!