magento - 将当前货币的订单金额转换为基础货币

时间:2013-12-31 06:27:24

标签: magento-1.7

我正在尝试将订单金额从当前货币转换为基础货币,下面是我尝试的代码,但没有去。

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

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

4 个答案:

答案 0 :(得分:6)

<强>更新

试试这段代码,可以帮到你。对于这种情况,我使用总金额。如果基础货币与当前的治愈率不同,则会发生变化。

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

答案 1 :(得分:6)

使用Mage::helper('directory')->currencyConvert($price, $baseCurrencyCode, $currentCurrencyCode)方法无法将当前货币的价格转换为基础货币,因为Magento找不到&#34; currentCurrency&#34; =&gt;&#34; baseCurrency&#34; directory_currency_rate 表中的关系。

要解决这个问题,您只需按照&#34; baseCurrency&#34; =&gt;&#34; currentCurrency&#34;进行价格划分。率。

你怎么能得到&#34; baseCurrency&#34; =&gt;&#34; currentCurrency&#34;率并解决您的问题? 这样:

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

答案 2 :(得分:0)

您的代码中需要进行少量修改。

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

如果您有疑问,请告诉我。

答案 3 :(得分:0)

您无需计算默认存储的基础货币的订单金额。检查sales_flat_order表中以base开头的列。