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

In this article, we will be guiding you on 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.

  • Share :