以编程方式在Magento中导入货币汇率

时间:2015-10-06 06:01:05

标签: php magento magento-1.9

我正在使用Magento 1.9.1.0。

我想以编程方式导入所有货币汇率,并且我想为所有可用货币添加x%额外费用。

// Code for Import Currency Rates
$currencyModel = Mage::getModel('directory/currency');
$currencies = $currencyModel->getConfigAllowCurrencies();
$baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
$defaultCurrencies = $currencyModel->getConfigBaseCurrencies();
$rates=$currencyModel->getCurrencyRates($defaultCurrencies, $currencies);
$percentage = 1.05;  // x% percentage (Example 5%)
foreach($rates[$baseCurrencyCode] as $CurrencyCode => $value  ) {
    $newValue = $value*$percentage;
    $newValue = round($newValue,4);
    $currencies = array($baseCurrencyCode => array($CurrencyCode => $newValue) );
    Mage::getModel('directory/currency')->saveRates($currencies);  // Update value in DB
}

如何从Webservicex导入货币汇率?

如果我可以将此代码放在上面的代码行之前,那就是我的目标。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我正在使用floatrates.com从欧元获得汇率:

$c = curl_init();
curl_setopt( $c, CURLOPT_URL,  'http://www.floatrates.com/daily/eur.xml');
curl_setopt( $c, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $c, CURLOPT_HTTPHEADER, array('Content-type: text/xml; charset=utf-8',));
curl_setopt( $c, CURLOPT_RETURNTRANSFER, true );
$xml_response = curl_exec($c);
curl_close($c);                

$sxml= new SimpleXMLElement($xml_response);

$rates= array();
foreach($sxml->item as $item) { 
    $rates[(string)$item->targetCurrency] = (1/ str_replace(',','',$item->exchangeRate));
    }