magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.56k stars 9.32k forks source link

Currency symbol position is not customizable #37280

Open ducquywp92 opened 1 year ago

ducquywp92 commented 1 year ago

Summary

Since Magento 2.4.6, Zendframework1 has been removed. We use below class to format price Magento\Framework\Currency\Data\Currency.php

On line 28 public const RIGHT = 16;

On line 139 $numberFormatter = new NumberFormatter($options['locale'], NumberFormatter::CURRENCY);

Position right value has been define, but never used. So if we using Japan locale, the output of this function alway be 円100. There is no option to move currency symbol (we expect 100円)

Examples

Proposed solution

Can we have the option to change the currency symbol position?

Release note

No response

Triage and priority

m2-assistant[bot] commented 1 year ago

Hi @ducquywp92. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

m2-assistant[bot] commented 1 year ago

Hi @engcom-Hotel. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-Hotel commented 1 year ago

Hello @ducquywp92,

Thanks for the report and collaboration!

We have gone through with the description and it seems to us a valid good-to-have feature.

Hence marking this issue as a feature request.

Thanks

akosglue commented 1 year ago

@ducquywp92 how did you solve it?

gmauro99 commented 1 year ago

@akosglue @ducquywp92

Correct scenario: Even after creating an observer and set the position symbol in \Magento\Framework\Currency::LEFT or RIGHT with setData the symbol is not moved.

This is only a temporary fix.

Just make on override of the method formatCurrency in (vendor/magento/module-directory/Model/Currency.php) at line 436 with:

        if ((array_key_exists(LocaleCurrency::CURRENCY_OPTION_DISPLAY, $options)
            && $options[LocaleCurrency::CURRENCY_OPTION_DISPLAY] === \Magento\Framework\Currency::NO_SYMBOL)) {
            $formattedCurrency = str_replace(' ', '', $formattedCurrency);
        }else if (array_key_exists('position', $options)
            && $options['position'] === \Magento\Framework\Currency::LEFT){
            $exp = explode(" ", $formattedCurrency);
            $formattedCurrency = $exp[1].' '.$exp[0];
        }else if (array_key_exists('position', $options)
            && $options['position'] === \Magento\Framework\Currency::RIGHT){
            $exp = explode(" ", $formattedCurrency);
            $formattedCurrency = $exp[0].' '.$exp[1];
         }
hieptvh18 commented 1 year ago

@akosglue @ducquywp92

Correct scenario: Even after creating an observer and set the position symbol in \Magento\Framework\Currency::LEFT or RIGHT with setData the symbol is not moved.

This is only a temporary fix.

Just make on override of the method formatCurrency in (vendor/magento/module-directory/Model/Currency.php) at line 436 with:

        if ((array_key_exists(LocaleCurrency::CURRENCY_OPTION_DISPLAY, $options)
            && $options[LocaleCurrency::CURRENCY_OPTION_DISPLAY] === \Magento\Framework\Currency::NO_SYMBOL)) {
            $formattedCurrency = str_replace(' ', '', $formattedCurrency);
        }else if (array_key_exists('position', $options)
            && $options['position'] === \Magento\Framework\Currency::LEFT){
            $exp = explode(" ", $formattedCurrency);
            $formattedCurrency = $exp[1].' '.$exp[0];
        }else if (array_key_exists('position', $options)
            && $options['position'] === \Magento\Framework\Currency::RIGHT){
            $exp = explode(" ", $formattedCurrency);
            $formattedCurrency = $exp[0].' '.$exp[1];
         }

its not working for me!

gmauro99 commented 1 year ago

@akosglue @ducquywp92 Correct scenario: Even after creating an observer and set the position symbol in \Magento\Framework\Currency::LEFT or RIGHT with setData the symbol is not moved. This is only a temporary fix. Just make on override of the method formatCurrency in (vendor/magento/module-directory/Model/Currency.php) at line 436 with:

        if ((array_key_exists(LocaleCurrency::CURRENCY_OPTION_DISPLAY, $options)
            && $options[LocaleCurrency::CURRENCY_OPTION_DISPLAY] === \Magento\Framework\Currency::NO_SYMBOL)) {
            $formattedCurrency = str_replace(' ', '', $formattedCurrency);
        }else if (array_key_exists('position', $options)
            && $options['position'] === \Magento\Framework\Currency::LEFT){
            $exp = explode(" ", $formattedCurrency);
            $formattedCurrency = $exp[1].' '.$exp[0];
        }else if (array_key_exists('position', $options)
            && $options['position'] === \Magento\Framework\Currency::RIGHT){
            $exp = explode(" ", $formattedCurrency);
            $formattedCurrency = $exp[0].' '.$exp[1];
         }

its not working for me!

have you added the observer?

hieptvh18 commented 1 year ago

@akosglue @ducquywp92 Correct scenario: Even after creating an observer and set the position symbol in \Magento\Framework\Currency::LEFT or RIGHT with setData the symbol is not moved. This is only a temporary fix. Just make on override of the method formatCurrency in (vendor/magento/module-directory/Model/Currency.php) at line 436 with:

        if ((array_key_exists(LocaleCurrency::CURRENCY_OPTION_DISPLAY, $options)
            && $options[LocaleCurrency::CURRENCY_OPTION_DISPLAY] === \Magento\Framework\Currency::NO_SYMBOL)) {
            $formattedCurrency = str_replace(' ', '', $formattedCurrency);
        }else if (array_key_exists('position', $options)
            && $options['position'] === \Magento\Framework\Currency::LEFT){
            $exp = explode(" ", $formattedCurrency);
            $formattedCurrency = $exp[1].' '.$exp[0];
        }else if (array_key_exists('position', $options)
            && $options['position'] === \Magento\Framework\Currency::RIGHT){
            $exp = explode(" ", $formattedCurrency);
            $formattedCurrency = $exp[0].' '.$exp[1];
         }

its not working for me!

have you added the observer?

yes, i have added observer!

esteban-w commented 7 months ago

I came across this issue while working on a feature, and as stated previously here (https://github.com/magento/magento2/issues/37280#issuecomment-1694432544), even if you create an observer to set the currency symbol 'position' data, the symbol is not affected because the 'position' data is never taken into account in the core currency format methods. Also, the method formatCurrency in "vendor/magento/module-directory/Model/Currency.php" mentioned here too (https://github.com/magento/magento2/issues/37280#issuecomment-1694432544), is a private method, which makes the option of creating a plugin for such a method impossible.

There is however a viable option that I used to solve this issue for the feature I was working on, which involves an after-plugin for the public method formatTxt in "vendor/magento/module-directory/Model/Currency.php". In fact, I created a very lightweight module publicly available that serves as a solution, so for anyone who has come across this same issue, you are very welcome to use that module or explore it to implement a similar solution https://github.com/esteban-w/m2-currency-symbol-position (at least until we get a permanent solution implemented by Magento in a future update). The solution implemented in the module basically consists of:

I hope this helps anyone looking for a solution, and if you need the details of my implementation you can find it at https://github.com/esteban-w/m2-currency-symbol-position

Peace.