3

How to display price currency symbol after the amount on the right side. from: USD333 to: 333USD

2
  • I hope my answer will solve your issue Commented Jun 9, 2018 at 11:05
  • Please share your code.
    – kunj
    Commented Jun 9, 2018 at 11:17

3 Answers 3

6

You can do it by writing single event observer

Create registration file:

Vendor\Module\registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

Create module file:

Vendor\Module\etc\module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0"></module>        
</config>

Create event File:

app/code/Vendor/Module/etc/events.xml

<?xml version="1.0"?>    
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="currency_display_options_forming">
        <observer name="change_currency_position" instance="Vendor\Module\Model\Observer\ChangeCurrencyPosition" />
    </event>
</config>

Create observer File:

Write a observer file in app/code/Vendor/Module/Model/Observer with the file name ChangeCurrencyPosition.php

<?php        
namespace Vendor\Module\Model\Observer;    
use Magento\Framework\Event\ObserverInterface;    
class ChangeCurrencyPosition implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {    
        $currencyOptions = $observer->getEvent()->getCurrencyOptions();    
        $currencyOptions->setData('position', \Magento\Framework\Currency::RIGHT);  
        return $this;
    }    
}

Run Command:

php bin/magento setup:upgrade

9
  • It did not work, Magento 2.2.3 and base currency is AED not USD Commented Jun 9, 2018 at 11:11
  • @AhmadDarwish what error you got after doing same? Commented Jun 9, 2018 at 11:15
  • No errors, nothing changing, where to trace the errors any log? Commented Jun 9, 2018 at 11:17
  • check error in var/log @AhmadDarwish Commented Jun 9, 2018 at 11:19
  • 1
    Thanks. Works in 2.4.1
    – Kristoffer
    Commented Jan 1, 2021 at 12:30
0

works in 2.4.2

-- app/code/Vendor/Module |-- etc | |-- events.xml | -- module.xml |-- Model | -- Observer | -- ChangeCurrencyPosition.php `-- registration.php

0

Try This

app/code/VendoreName/ModuleName/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">

    <preference for="Magento\Directory\Model\Currency" type="VendoreName\ModuleName\Model\Currency" />

</config>

app/code/VendoreName/ModuleName/Model

Currency.php

<?php

namespace VendoreName\ModuleName\Model;

class Currency extends \Magento\Directory\Model\Currency
{
    public function formatTxt($price, $options = [])
    {
        if (!is_numeric($price)) {
            $price = $this->_localeFormat->getNumber($price);
        }
        $price = sprintf("%F", $price);

        return $this->_localeCurrency->getCurrency($this->getCode())->toCurrency($price, $options);
    }

}

app/code/VendoreName/ModuleName/etc

events.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

    <event name="currency_display_options_forming">
        <observer name="change_currency_position" instance="VendoreName\ModuleName\Observer\ChangeCurrencyPosition" />
    </event>

</config>

app/code/VendoreName/ModuleName/Observer

ChangeCurrencyPosition.php

<?php

namespace VendoreName\ModuleName\Observer;

use Magento\Framework\Event\ObserverInterface;

class ChangeCurrencyPosition implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $currencyOptions = $observer->getEvent()->getCurrencyOptions();
        $currencyOptions->setData('position', \Magento\Framework\Currency::LEFT);
        //$currencyOptions->setData('position', \Magento\Framework\Currency::RIGHT);
        return $this;
    }
}

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