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


Programmatically import Currency Rates in Magento

我使用的是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导入货币汇率?

如果我能把这段代码放在上面的代码行之前,那就是我的目标。

有什么想法吗?

我正在使用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));
    }