How to display price currency symbol after the amount on the right side. from: USD333 to: 333USD
3 Answers
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
-
It did not work, Magento 2.2.3 and base currency is AED not USD Commented Jun 9, 2018 at 11:11
-
-
No errors, nothing changing, where to trace the errors any log? Commented Jun 9, 2018 at 11:17
-
-
1
works in 2.4.2
-- app/code/Vendor/Module |-- etc | |-- events.xml |
-- module.xml
|-- Model
| -- Observer |
-- ChangeCurrencyPosition.php
`-- registration.php
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;
}
}