Back to Top

Price conversion from one currency to another

Updated 26 July 2023

Here is how you can convert the price from one currency to another.

First of all, in the Constructor, create an instance of  “\Magento\Directory\Model\Currency” as below:

public function __construct(
    \Magento\Framework\App\Helper\Context $context,
     \Magento\Directory\Model\Currency $currency
) {
        parent::__construct($context);
        $this->_currency = $currency;
}

then you can use the following function to get the updated price in the new currency.

public function getNewPrice($price, $currCodeFrom, $currCodeTo)
{
    $rate = $this->_currency->load($currCodeFrom)->getAnyRate($currCodeTo);
    $newPrice = $price * $rate;
    return newPrice;
}

here, $price is the amount that you want to convert, $currCodeFrom is the currency code from which the price has to be converted, and $currCodeTo is the currency Code in which you want the converted price.

I have used this code in a helper, you can use this in Block or a Controller as per your need.

Searching for an experienced
Magento 2 Company ?
Find out More

For any queries, feel free to comment below. 🙂

Moreover, using Magento 2 GeoIP module you can automatically change currency and select the store view based on the customer’s location.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home