Magento, Magento 2

How to Change Currency Position From Left to Right in Magento 2

How to Change Currency Position From Left to Right in Magento 2

In this tutorial, Today I will explain to how to change currency position in Magento 2. In many website, we need to change currency from left to right for whole website. By default, In Magento 2 it’s display left side. So, How to change currency position from left to right.

Let’s follow the below steps for that.

You may also like this :

1) Let’s assume that you have created simple module. Now, you need to create di.xml file at below path app/code/RH/Helloworld/etc/ and paste the below code :

<?xml version="1.0"?>
<!--
/**
 * Created By : Rohan Hapani
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Directory\Model\Currency">
        <plugin name="change_currency_position"
            type="RH\Helloworld\Plugin\ChangeCurrencyPosition"/>
    </type>
</config>

2) Now, You need to create ChangeCurrencyPosition.php file at app/code/RH/Helloworld/Plugin/ and paste the below code to add code logic :

<?php
/**
 * Created By : Rohan Hapani
 */
namespace RH\Helloworld\Plugin;

class ChangeCurrencyPosition
{
    /**
     * Change currency position
     *
     * @param \Magento\Directory\Model\Currency $subject
     * @param float $price
     * @param array $options
     * @return array|array[]
     */
    public function beforeFormatTxt(
        \Magento\Directory\Model\Currency $subject,
        $price,
        $options = []
    ) {
        $options['position'] = 16; // 16 is for Right position, 8 is for Standard position and 32 is for Left position
        return [$price,$options];
    }
}

Here, we created Magento\Directory\Model\Currency::formatTxt() function’s before plugin.

That’s it !!!

Now, You just need to clean cache and check it. You can see currency position will be show at right side in whole website.

I hope this blog is easy to understand about how to change currency position in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution.

Keep liking and sharing !!

Tagged ,