Magento – convert amount form one currency to another currency in magento 2

currencymagento2

I need to convert amount from one currency to another currency in magento 2 not from store base currency

$priceCurrencyObject = $objectManager->get('Magento\Framework\Pricing\PriceCurrencyInterface'); //instance of PriceCurrencyInterface


$store = $this->_storeManager->getStore()->getStoreId(); //get current store id if store id not get passed

$rate = $priceCurrencyObject->convert($USDamount, $store, 'INR');

I tried above code it will convert base currency to another currency.

My criteria is I need to mention from currency and to currency.

Is it possible in magento2

Best Answer

You can use this code (or whole class) to convert from the one currency to another:

<?php

/**
 * Created by PhpStorm.
 * User: siarhey
 * Date: 8/26/17
 * Time: 7:25 AM
 */
class ConvertCurrencyTest
{
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;

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

    /**
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
     */
    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Directory\Model\CurrencyFactory $currencyFactory
    ){
        $this->storeManager = $storeManager;
        $this->currencyFactory = $currencyFactory;
    }

    /**
     * Converts the amount value from one currency to another.
     * If the $currencyCodeFrom is not specified the current currency will be used.
     * If the $currencyCodeTo is not specified the base currency will be used.
     * 
     * @param float $amountValue like 13.54
     * @param string|null $currencyCodeFrom like 'USD'
     * @param string|null $currencyCodeTo like 'BYN'
     * @return float
     */
    public function convert($amountValue, $currencyCodeFrom = null, $currencyCodeTo = null)
    {
        /**
         * If is not specified the currency code from which we want to convert - use current currency
         */
        if (!$currencyCodeFrom) {
            $currencyCodeFrom = $this->storeManager->getStore()->getCurrentCurrency()->getCode();
        }

        /**
         * If is not specified the currency code to which we want to convert - use base currency
         */
        if (!$currencyCodeTo) {
            $currencyCodeTo = $this->storeManager->getStore()->getBaseCurrency()->getCode();
        }

        /**
         * Do not convert if currency is same
         */
        if ($currencyCodeFrom == $currencyCodeTo) {
            return $amountValue;
        }

        /** @var float $rate */
        // Get rate
        $rate = $this->currencyFactory->create()->load($currencyCodeFrom)->getAnyRate($currencyCodeTo);
        // Get amount in new currency
        $amountValue = $amountValue * $rate;

        return $amountValue;
    }
}

Additional, if you do not specify the currency from which you want to conver - current currency will be used. If you do not specify the currency in which you want to convert - base currency will be used.

Related Topic