Magento 2 Change Currency Symbol Position

By default, Magento 2 shows the price like this (€90.00) with EUR currency. We expect to change the symbol next to number (90.00€)
I looked around the Core files to find the best way to do it, and I found this file vendor/magento/module-currency-symbol/Observer/CurrencyDisplayOptions.php
It hooks to event “currency_display_options_forming” to change the currency options.
Oh yea, we will do like this way to change the position.

– I already have a basic module Sutunam/Catalog
1. We create a new Observer file (app/code/Sutunam/Catalog/Observer/CurrencyDisplayOptions.php)

<?php

namespace Sutunam\Catalog\Observer;

use Magento\Framework\Event\ObserverInterface;

class CurrencyDisplayOptions implements ObserverInterface
{
    /**
     * hook to event currency_display_options_forming
     * change currency symbol position
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return $this
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $baseCode = $observer->getEvent()->getBaseCode();
        $currencyOptions = $observer->getEvent()->getCurrencyOptions();

        if ($baseCode === 'EUR') { // change currency symbol position to Right with EUR
            $currencyOptions->setData('position', \Magento\Framework\Currency::RIGHT);
        }

        return $this;
    }
}

These lines to change the position

if ($baseCode === 'EUR') { // change currency symbol position to Right with EUR
    $currencyOptions->setData('position', \Magento\Framework\Currency::RIGHT);
}

My basecode is EUR, you can change it by your basecode or remove this IF.

$currencyOptions->setData(‘position’, \Magento\Framework\Currency::RIGHT); <= This line to change the currency symbol position

‘position’ = 16 (right)
‘position’ = 32 (left)

2. We create events.xml file (app/code/Sutunam/Catalog/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="sutunam_catalog_currency_display_options" instance="Sutunam\Catalog\Observer\CurrencyDisplayOptions" />
    </event>
</config>

instance=”Sutunam\Catalog\Observer\CurrencyDisplayOptions” <= this is the file path that you created on step 1.

Enjoy your code 😉

Leave a Reply