2

How Convert USD to INR ( base currency) in magento?. I tried to below code , but not working.

$amt=1000;
Mage::helper('directory')->currencyConvert($amt, 'USD', 'INR'); 

But its work on Mage::helper('directory')->currencyConvert($amt, 'INR', 'USD');

5
  • this price in inr
    – Amit Bera
    Sep 3, 2014 at 10:32
  • No, Price in USD. i need to current currency (USD) to Base currency ( INR )
    – Rat
    Sep 3, 2014 at 10:34
  • clear cache.....
    – Amit Bera
    Sep 3, 2014 at 10:39
  • is this price 1000 in USD?
    – Amit Bera
    Sep 3, 2014 at 10:40
  • yes , USD Price
    – Rat
    Sep 3, 2014 at 10:42

2 Answers 2

4

You cannot convert one price currency price to base currency.

because magento did not calculate base currency rate from other current rate.

Magento define a price in BASE currency which will convert using rate for other currency

Magento is calculate other price from base Currency

Please got see Mage_Directory_Model_Currency class on function

 public function convert($price, $toCurrency=null)
    {
        if (is_null($toCurrency)) {
            return $price;
        }
        elseif ($rate = $this->getRate($toCurrency)) {
            return $price*$rate;
        }

        throw new Exception(Mage::helper('directory')->__('Undefined rate from "%s-%s".', $this->getCode(), $toCurrency->getCode()));
    }

Here show that you can not get base currency from other currency

That means that you code format is

Mage::helper('directory')->currencyConvert($amt, 'baseCurrecy', 'your currency');

revise it not possible using this function

Mage::helper('directory')->currencyConvert($amt, 'your currency', 'baseCurrecy');

1
  • Is it possible any other way to convert?
    – Rat
    Sep 3, 2014 at 10:58
4

It is not possible to convert a price from current currency to base currency using Mage::helper('directory')->currencyConvert($amt, 'USD', 'INR') method because Magento doesn't find the row with "currentCurrency"=>"baseCurrency" relation in the directory_currency_rate table.

To solve this issue you can simply do a division of the price by "baseCurrency"=>"currentCurrency" rate.

How could you get "baseCurrency"=>"currentCurrency" rate and solve your issue? In this way:

// the price
$amt=1000;
// Base Currency ('INR')
$baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
// Current Currency ('USD')
$currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();

// Allowed currencies
$allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies();
$rates = Mage::getModel('directory/currency')->getCurrencyRates($baseCurrencyCode, array_values($allowedCurrencies));
// the price converted
$amt= $amt/$rates[$currentCurrencyCode];
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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