1、获取当前店铺的货币方法:
当前店铺的基准货币:$baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
$CurrentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
$CurrentCurrencySymnol = Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol();
$CurrentCurrencyName = Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getName();
2、获取自定义货币的符号,code,返回的是一个二维数组
$currencyDate = Mage::getModel('currencysymbol/system_currencysymbol')->getCurrencySymbolsData();
//获取系统默认的结算币种
$baseCurrencyCode = Mage::app()->getBaseCurrencyCode();
//获取所有系统可用结算币种
$allowedCurrencies = Mage::getModel('directory/currency')
->getConfigAllowCurrencies();
//获取两个币种之间的汇率
$currencyRates = Mage::getModel('directory/currency')
->getCurrencyRates($baseCurrencyCode, array_values($allowedCurrencies));
下面是几个常用的货币转换方法。需要注意的是,凡是涉及到两个币种之间转换的,都必须确保系统中已经导入了两币种之间的汇率。更多币种之间的方法可以到directory/data助手类中找到。
// 将100人民币转换为美金
$Price = Mage::helper('directory')->currencyConvert(100, 'CNY', 'USD');
//将100美金转换为人民币
$Price = Mage::helper('directory')->currencyConvert(100, 'USD', 'CNY');
汇率的导入:
magento后台currency rate页面有个Import按钮:
点击import导入货币汇率。
汇率导入的方法:
class Mage_Adminhtml_System_CurrencyController extends Mage_Adminhtml_Controller_Action
{
public function fetchRatesAction()
{
try {
$service = $this->getRequest()->getParam('rate_services');/*后台Import Service 的值*/
$this->_getSession()->setCurrencyRateService($service);
if( !$service ) {
throw new Exception(Mage::helper('adminhtml')->__('Invalid Import Service Specified'));
}
try {/*实例化Webservicex这个Model,magento自带的是Webservicex*/
$importModel = Mage::getModel(
Mage::getConfig()->getNode('global/currency/import/services/' . $service . '/model')->asArray()
);
} catch (Exception $e) {
Mage::throwException(Mage::helper('adminhtml')->__('Unable to initialize import model'));
}
$rates = $importModel->fetchRates();
$errors = $importModel->getMessages();
if( sizeof($errors) > 0 ) {
foreach ($errors as $error) {
Mage::getSingleton('adminhtml/session')->addWarning($error);
}
Mage::getSingleton('adminhtml/session')->addWarning(Mage::helper('adminhtml')->__('All possible rates were fetched, please click on "Save" to apply'));
} else {
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('All rates were fetched, please click on "Save" to apply'));
}
Mage::getSingleton('adminhtml/session')->setRates($rates);
}
catch (Exception $e){
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
$this->_redirect('*/*/');
}
}
下面是Webservicx的文件路径:
说明:
$rates = $importModel->fetchRates();这不调用的方法在下面可以找到:
abstract class Mage_Directory_Model_Currency_Import_Abstract
{
/*Webservicex继承这个Model*/
public function fetchRates()
{
$data = array();
$currencies = $this->_getCurrencyCodes();/*获取website所有允许的货币code*/
$defaultCurrencies = $this->_getDefaultCurrencyCodes();/*获取website中默认货币code*/
@set_time_limit(0);/*设置程序执行时间,为零表示,执行直到程序结束(因为要去第三方获取资源,所以较长)*/
foreach ($defaultCurrencies as $currencyFrom) {
if (!isset($data[$currencyFrom])) {
$data[$currencyFrom] = array();
}
foreach ($currencies as $currencyTo) {
if ($currencyFrom == $currencyTo) {
$data[$currencyFrom][$currencyTo] = $this->_numberFormat(1);
}
else {
$data[$currencyFrom][$currencyTo] = $this->_numberFormat($this->_convert($currencyFrom, $currencyTo));
}
}
ksort($data[$currencyFrom]);
}
return $data;
}
}
根据以上几个方法,可以写一个定时任务,按时更新store的货币汇率