1

I am converting price from any other currency using currency code to base currency and for this I am using this method :

$oldCurrency = 'AUD';
$newCurrency = 'USD';
$priceHelper = $objectManager->create('Magento\Directory\Helper\Data');
$priceHelper->currencyConvert($item->getCustomPrice(), $oldCurrency, $newCurrency); 

My base currency is USD and USD to AUD conversion rate is defined in admin.

When I am converting price from USD to AUD then its working, but when I am trying to convert price from AUD to USD then it gives me error that conversion rate is not defined.

How do I convert price from any currency to any other currency ?

4

NOTE: In the example the amount in current store currency is converting to amount in base store currency.

You can use this code to convert one currency to another:

/**
 * @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;
}

/**
 * Convert base price value to store price value
 *
 * @param $amountValue
 * @return float
 */
public function convertPrice($amountValue)
{
    $currentCurrency = $this->storeManager->getStore()->getCurrentCurrency()->getCode();
    $baseCurrency = $this->storeManager->getStore()->getBaseCurrency()->getCode();
    if ($currentCurrency != $baseCurrency) {
        $rate = $this->currencyFactory->create()->load($currentCurrency)->getAnyRate($baseCurrency);
        $amountValue = $amountValue * $rate;
    }

    return $amountValue;
}

The $currentCurrency and $baseCurrency variables can be replaced by any valid currency codes from the currency collection.

1

You need to setup AUD in store allow currency

Admin >> Store >> Configuration >> General >> General >> Allowed Currencies - Select AUD and USD and save.

Now you can see your currency rate here

Admin >> Store >> Currency >> Currency Rates >> Import >> Save Currency Rates.

Now you can check your custome currency contvert code.

  • These all things are already done and default currency converter is working properly. – Ashish Jagnani Jun 27 '17 at 7:51
1

For converting any currency, your currency must be in the allowed currency
Admin >> Store >> Configuration >> General >> General >> Allowed Currencies

Your currency rate must be set in currency rates
Admin >> Store >> Currency >> Currency Rates

Magento Directory provides conversion from Base currency to any other currency.
But if you want to convert currency from any currency, then you can use Magento \Magento\Directory\Model\Currency class.

 /**
 * constructor.
 * @param \Magento\Directory\Model\Currency $currency
 */
public function __construct(
    \Magento\Directory\Model\Currency $currency
) {
    $this->currency = $currency;
}


/**
 * convertCurrency.
 * @param  string $from
 * @param  string $to
 * @param  float $amount
 */
public function convertCurrency($from,$to,$amount){
    if($from == $to)
        return $amount;
    $this->currency->load($from);
    $rate = $this->currency->getAnyRate($to);
    return $rate*$amount;

}
0

This means it cannot find the correct rate. I would suggest going to Store >> Currency >> Currency Rates and click on Import button which should resolve your issue if the rate has been selected in the system config

  • These all things are already done and default currency converter is working properly. – Ashish Jagnani Jun 27 '17 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.