0

In product details page, The currency symbol shows at starting of the product price (₹125.00).

I need to show the currency symbol at end of the product price(125.00 ₹) in product details page.

i.e ., Change ₹125.00 to 125.00 ₹.

1

2 Answers 2

4

You can easily do this by just writing single event, First write events.xml file in app>code>YOUR_VENDOR_ NAME>YOUR_MODULE_NAME>etc

<?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="YOUR_VENDOR\YOUR_MDOULENAME\Observer\CurrencyPositionChange" />
    </event>
</config>

Write a observer file in app>code>YOUR_VENDOR_ NAME>YOUR_MODULE_NAME>Observer with the file name CurrencyPositionChange.php

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

that's it and don't forgot to clear cache by

php bin/magento c:f

5
  • 1
    While printing the $currencyOptions the result as Magento\Framework\DataObject Object ( [_data:protected] => Array ( [position] => 16 [symbol] => ) ) but it not reflecting in the dispay result.
    – vinoth
    Jun 12, 2017 at 11:53
  • See my edit and clear cache you can see the result, It's working in my system @vinoth
    – Keyur Shah
    Jun 12, 2017 at 12:01
  • 1
    I have cleared cache, but it is not affecting.
    – vinoth
    Jun 12, 2017 at 12:49
  • Its working for me thanks @KeyurShah But when I used this my currency switcher not working any reason?
    – Rajan Soni
    Apr 6, 2018 at 5:45
  • @KeyurShah And here I've been writing "cache:flush" the whole time like a sucker!
    – micwallace
    Apr 24, 2020 at 14:17
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.