3

The store locale is bg_BG. After the product page is opened the price is rendered correctly formatted from the server. When the following JS is executed the format is wrong.

<script>
    require([
        'jquery',
        'priceBox'
    ], function($){
        var dataPriceBoxSelector = '[data-role=priceBox]',
            dataProductIdSelector = '[data-product-id=1]',
            priceBoxes = $(dataPriceBoxSelector + dataProductIdSelector);

        priceBoxes = priceBoxes.filter(function(index, elem){
            return !$(elem).find('.price-from').length;
        });

        priceBoxes.priceBox({'priceConfig': {"productId":"1","priceFormat":{"pattern":"%s\u00a0\u043b\u0432.","precision":2,"requiredPrecision":2,"decimalSymbol":",","groupSymbol":"\u00a0","groupLength":2,"integerRequired":false}}});
    });
</script>

I cannot find what is causing this issue and how to fix it. Any guidance would be really appreciated.

Examples: Before JS: 1 199,00 лв. 155,00 лв. after the JS: 11 99,00 лв. 1 55,00 лв.

  • I updated my answer, check if works for you now – gemig_hol Dec 18 '18 at 15:55
  • Did my answer solve your problem @kolek? – gemig_hol Jan 18 '19 at 21:47
2

You can make check for currency in the Plugin.

Create etc/frontend/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">
    <type name="Magento\Framework\Locale\FormatInterface">
        <plugin name="{pluginName}" type="{Vendor}\{ModuleName}\Plugin\Framework\Locale\Format" />
    </type>
</config>

Create {Vendor}\{ModuleName}\Plugin\Framework\Locale\Format class:

namespace {Vendor}\{ModuleName}\Plugin\Framework\Locale;

/**
 * Class Format
 * @package {Vendor}\{ModuleName}\Plugin\Framework\Locale
 */
class Format
{
    /**
     * @var \Magento\Framework\App\ScopeResolverInterface
     */
    private $scopeResolver;

    public function __construct(\Magento\Framework\App\ScopeResolverInterface $scopeResolver)
    {
        $this->scopeResolver = $scopeResolver;
    }

    /**
     * @param \Magento\Framework\Locale\FormatInterface $subject
     * @param $result
     * @return mixed
     */
    public function afterGetPriceFormat(
        \Magento\Framework\Locale\FormatInterface $subject,
        $result
    ) {
        $currency = $this->scopeResolver->getScope()->getCurrentCurrency();

        if ($currency->getCode() == 'BGN') {
            $result['groupLength'] = 3;
        }

        return $result;
    }
}
| improve this answer | |
1

If you want to set the prices grouped by 3  (1 199,00 лв) you need to set the groupLength property from priceBox to 3 :

UPDATE

Improving my answer, you can create an after plugin for \Magento\Framework\Locale\Format and set there the groupLength to 3:

Create etc/frontend/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">
    <type name="Magento\Framework\Locale\Format">
        <plugin name="formatConfig"
                type="< Vendor>\< ModuleName>\Plugin\Locale\Format"
                sortOrder="100"
        />
    </type>

</config>

Create < Vendor>\< ModuleName>\Plugin\Locale\Format :

<?php

namespace < Vendor>\< ModuleName>\Plugin\Locale;

class Format
{
    public function afterGetPriceFormat(\Magento\Framework\Locale\Format $subject, $result)
    {
        $result["groupLength"] =3;
        return $result;
    }
}

BEFORE:

enter image description here

AFTER

enter image description here

Hope it helps.

| improve this answer | |
  • Sure, but the format options are dynamically generated in PHP based on the current locale, so this is not a solution. – kolek Dec 17 '18 at 7:58
0

What I found but was not able to resolve correctly is that the format of the price is not returned correctly by NumberFormatter->getPattern on line 102 magento/framework/Locale/Format.php. The only reason I see for this is the ICU Version of the Intl extension. For now, I was able to test it with ICU 60.2, 60.3. I'm having the issue with both of the versions and I know it is ok with 50, 55, 57

Hope it may help someone else with the same issue and if you find a solution please reply to that thread.

| improve this answer | |
0

There аре only 2 options to solve the problem:

  1. Not good one: Compile php-intl with ICU 57.1 maximum. All versions after this one have broken currency pattern for bg_BG locale. The correct patterns should be "#,##0.00 ¤". Here are instructions how to do this

  2. Good one: Create custom class overriding magento Format class. Here are some instructions how to do this

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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