0

Want to convert number format into currency in mini cart. Was able to convert it into PHP but it gets converted into another format due to JS

Number 1370000 convert to $1.370.000,00

Magento2 : Manage currency format in multi-language website

1 Answer 1

2

First create di.xml

<?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="Adamsmage\Priceformat\Model\Format" />
</config>

second create model file at app/code/Adamsmage/Priceformat/Model/Format.php    

<?php
namespace Adamsmage\Priceformat\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]);
    // 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;
}}
1
  • thx but if i am not wrong it would be via php. Mini cart gets updated via Ajax call. Commented Jun 18, 2019 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.