How to convert the price from one currency to another in the Magento 2?

Aug 3rd, 2018 2 min to read

We discussed how to set custom prices programmatically in the last post. Now, in this article, we will be guiding you on one of the most frequently asked queries related to Magento web development. That is, how to convert the price from one currency to another in the magento 2.

The first thing you need to do is to create an instance of “\Magento\Directory\Model\CurrencyFactory” and “\Magento\Store\Model\StoreManagerInterface” as given below:

/**
* @var \Magento\Directory\Model\CurrencyFactory
*/
public $currencyFactory;

/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
public $storeManager;

/**
* Data constructor.
*
* @param \Magento\Framework\App\Helper\Context $context
* @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
*/
public function __construct(
   \Magento\Framework\App\Helper\Context $context,
   \Magento\Directory\Model\CurrencyFactory $currencyFactory,
   \Magento\Store\Model\StoreManagerInterface $storeManager
) {
   $this->currencyFactory = $currencyFactory;
   $this->storeManager = $storeManager;
   parent::__construct($context);
}

Then you need to use the function given below to get the price converted from one currency to another currency.

public function convertPrice($price, $currencyCodeFrom, $currencyCodeTo)
{
   $rate = $this->currencyFactory->create()
                ->load($currencyCodeFrom)
                ->getAnyRate($currencyCodeTo);
   
   $convertedPrice = $price * $rate;

   return $convertedPrice;
}

In the snippet above,
$price : amount that you want to convert
$currencyCodeFrom : currency code from which the price has to be converted
$currencyCodeTo : currency code in which you want the price to be converted

Convert price from current currency to base currency

You need to use the following function to get the converted price from current currency to base currency.

public function convertPriceFromCurrentCurrencyToBaseCurrency($price)
{
   $curentCurrencyCode =  $this->storeManager->getStore()
                               ->getCurrentCurrency()
                               ->getCode();
   $baseCurrencyCode =  $this->storeManager->getStore()
                             ->getBaseCurrency()
                             ->getCode();

   $rate = $this->currencyFactory->create()
                 ->load($curentCurrencyCode)
                 ->getAnyRate($baseCurrencyCode);

   $convertedPrice = $price * $rate;

   return $convertedPrice;
}

Convert price from current currency to another currency

You need to use the following function to get the converted price from current currency to another currency.

public function convertPriceFromCurrentToAnotherCurrency($price, $currencyCodeTo)
{
   $curentCurrencyCode =  $this->storeManager->getStore()->getCurrentCurrency()->getCode();
   $rate = $this->currencyFactory->create()
                ->load($curentCurrencyCode)
                ->getAnyRate($currencyCodeTo);

   $convertedPrice = $price * $rate;

   return $convertedPrice;
}

In this example, I have implemented this code in the helper, but you can also implement this in a Block or a Controller as per your requirement.

Also read: How to add a column to tier pricing in Magento 2

Speak your Mind

Post a Comment

Got a question? Have a feedback? Please feel free to leave your ideas, opinions, and questions in the comments section of our post! ❤️

* This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.