Saat ini harga menunjukkan seperti $ 2,999,00
Saya ingin harga ditampilkan seperti $ 2,999.00 untuk es_MX lokal (Spanyol, Meksiko) di halaman produk , di mana pun format mata uangnya benar.
Saya sudah mencoba semua solusi di stackexchange tetapi tidak ada yang berhasil.
File aplikasi / kode / Jsp / Mata Uang / etc / di.xml
<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="Jsp\Currency\Model\Format"/>
</config>
File aplikasi / kode / Jsp / Mata Uang / Model / Format.php
<?php
namespace Jsp\Currency\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.....
if($localeCode == 'es_MX'){
$decimalSymbol = '.';
$groupSymbol = ',';
}else{
$decimalSymbol = $localeData['NumberElements'][$defaultSet]['symbols']['decimal']
?: ($localeData['NumberElements'][self::$defaultNumberSet]['symbols']['decimal']
?: $localeData['NumberElements'][0]);
$groupSymbol = $localeData['NumberElements'][$defaultSet]['symbols']['group']
?: ($localeData['NumberElements'][self::$defaultNumberSet]['symbols']['group']
?: $localeData['NumberElements'][1]);
}
$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;
}
}
Vendor file / magento / framework / Lokal / Format.php
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Locale;
use Magento\Framework\Locale\Bundle\DataBundle;
class Format implements \Magento\Framework\Locale\FormatInterface
{
/**
* @var string
*/
private static $defaultNumberSet = 'latn';
/**
* @var \Magento\Framework\App\ScopeResolverInterface
*/
protected $_scopeResolver;
/**
* @var \Magento\Framework\Locale\ResolverInterface
*/
protected $_localeResolver;
/**
* @var \Magento\Directory\Model\CurrencyFactory
*/
protected $currencyFactory;
/**
* @param \Magento\Framework\App\ScopeResolverInterface $scopeResolver
* @param ResolverInterface $localeResolver
* @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
*/
public function __construct(
\Magento\Framework\App\ScopeResolverInterface $scopeResolver,
\Magento\Framework\Locale\ResolverInterface $localeResolver,
\Magento\Directory\Model\CurrencyFactory $currencyFactory
) {
$this->_scopeResolver = $scopeResolver;
$this->_localeResolver = $localeResolver;
$this->currencyFactory = $currencyFactory;
}
/**
* Returns the first found number from an string
* Parsing depends on given locale (grouping and decimal)
*
* Examples for input:
* ' 2345.4356,1234' = 23455456.1234
* '+23,3452.123' = 233452.123
* ' 12343 ' = 12343
* '-9456km' = -9456
* '0' = 0
* '2 054,10' = 2054.1
* '2'054.52' = 2054.52
* '2,46 GB' = 2.46
*
* @param string|float|int $value
* @return float|null
*/
public function getNumber($value)
{
if ($value === null) {
return null;
}
if (!is_string($value)) {
return floatval($value);
}
//trim spaces and apostrophes
$value = str_replace(['\'', ' '], '', $value);
$separatorComa = strpos($value, ',');
$separatorDot = strpos($value, '.');
if ($separatorComa !== false && $separatorDot !== false) {
if ($separatorComa > $separatorDot) {
$value = str_replace('.', '', $value);
$value = str_replace(',', '.', $value);
} else {
$value = str_replace(',', '', $value);
}
} elseif ($separatorComa !== false) {
$value = str_replace(',', '.', $value);
}
return floatval($value);
}
/**
* Functions returns array with price formatting info
*
* @param string $localeCode Locale code.
* @param string $currencyCode Currency code.
* @return array
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
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]);
$decimalSymbol = $localeData['NumberElements'][$defaultSet]['symbols']['decimal']
?: ($localeData['NumberElements'][self::$defaultNumberSet]['symbols']['decimal']
?: $localeData['NumberElements'][0]);
$groupSymbol = $localeData['NumberElements'][$defaultSet]['symbols']['group']
?: ($localeData['NumberElements'][self::$defaultNumberSet]['symbols']['group']
?: $localeData['NumberElements'][1]);
$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;
}
}
Jawaban:
buat hanya modul sederhana dan overrider default * file Format.php **,
app / code / Package / Modulename / etc / di.xml
buat file model, aplikasi / kode / Paket / Modulename / Model / Format.php
Terima kasih.
sumber
Gunakan kode di bawah ini:
Format fungsi seperti di bawah ini:
Jika $ includeContainer = true maka harga akan ditampilkan dengan span span
$precision = self::DEFAULT_PRECISION
Ini akan menampilkan dua titik desimal. Menggunakan 0 itu tidak akan menampilkan titik desimal.sumber
Secara default Magento 2, format harga agak aneh untuk beberapa mata uang sehingga kami perlu mengubahnya. Inilah cara untuk mengubah format harga.
Inilah contoh untuk kasus dong Vietnam. Format default yang ditampilkan adalah 100.000,00. Lalu saya mengubahnya menjadi 100.000 (dipisahkan dengan koma tanpa titik desimal).
Terima kasih menikmati :)
sumber
<currencyFormat
Anda dapat mengatur format seperti ini:
sumber
untuk mengubah atau menghapus desimal untuk berbagai mata uang, Anda hanya perlu menginstal modul Extension Formatter Mata Uang gratis dari Mageplaza di sini tautannya: https://www.mageplaza.com/magento-2-currency-formatter/ .
Daripada Anda dapat mengkonfigurasi desimal untuk kebutuhan Anda dari panel admin magento -> store-> konfigurasi-> Mageplaza Extensions.
Ini bekerja untuk saya di instalasi magento 2.3.3.
salam Hormat
sumber
Cara buruk untuk melakukannya (tetapi lebih cepat) adalah vendor hardcoding / magento / framework / Lokal / Format.php
sumber