Deepak Mallah
Deepak Mallah

Reputation: 4076

magento - convert order amount from current currency to base currency

I am trying to convert order amount from current currency to base currency, below is the code which i tried, but no go.

$baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode(); 
$currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode(); 
$price = 1; 

$priceTwo = Mage::helper('directory')->currencyConvert($price, $baseCurrencyCode, $currentCurrencyCode); 

Upvotes: 3

Views: 10884

Answers (4)

WaPoNe
WaPoNe

Reputation: 336

It is not possible to convert a price from current currency to base currency using Mage::helper('directory')->currencyConvert($price, $baseCurrencyCode, $currentCurrencyCode) 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
$price=1;
// Base Currency
$baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
// Current Currency
$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
$price=$price/$rates[$currentCurrencyCode];

Upvotes: 6

hendrathings
hendrathings

Reputation: 3765

UPDATE

try this code, may help you. for this case I use total amount. and will change if the base currency different with current curency.

    $amount = $this->getOrder()->getGrandTotal();

    $baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
    $currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();

    if ($baseCurrencyCode != $currentCurrencyCode) {
        // convert price from current currency to base currency
        $amount = Mage::helper('directory')->currencyConvert($amount, $currentCurrencyCode, $baseCurrencyCode);

        // convert price from base currency to current currency
        //$priceTwo = Mage::helper('directory')->currencyConvert($amount, $baseCurrencyCode, $currentCurrencyCode);
    }

    $amountFinal = round($amount, 2);

Upvotes: 6

Simon H
Simon H

Reputation: 2495

You don't need to calculate the order amount of the base currency as it is stored by default. Check the columns starting with base in the sales_flat_order table.

Upvotes: 0

Amit Bera
Amit Bera

Reputation: 7611

A small modification need in your code.

$baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode(); 
$currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode(); 
$price = 1; // price should be in  base CurrencyCode 
$precision=2; //float point of price
$PriceTwo=Mage::app()->getLocale()->currency($currentCurrencyCode )->toCurrency($price,array('precision'=>$precision));

Let me know if you have query.

Upvotes: 0

Related Questions