magento - convert order amount from current currency to base currency

两盒软妹~` 提交于 2020-01-01 03:36:34

问题


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); 

回答1:


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);



回答2:


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];



回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/20852882/magento-convert-order-amount-from-current-currency-to-base-currency

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!