大多数magent用户有多个货币显示在他们的网站上已经注意到,建于Webservicex货币服务停止工作前一段时间(2013年底)。
搜索了一段时间后,我们发现一篇文章展示了如何添加雅虎金融货币转换服务线上购物,然而,从谈话中得知,这违反了雅虎的T&C所以它并不是一个选项。同样的,谷歌的货币转换服务已经停止。因此,离开选项是什么?
更多的搜索后,我们发现Openexchangerates聽提供一个免费的和几个级别的优质服务。自由选项允许每月1000 API请求,我们认为足够多的线上购物的标准用例。比方说一个月有31天,这将允许每天大约32请求这意味着你可以货币服务更新你的网站每45分钟,而不是每月的访问限制。所以,注册一个免费账户,让您的应用程序Id。
所有这些都是好消息,我们创建了一个快速扩展,添加该服务线上购物。
扩展结构
假设我们的读者是熟悉创建一个扩展线上购物,我们在这里展示相关的代码,让你把它组装成一个可安装的模块。
首先我们想定义模块名称空间,所以我们称之为 Magebase_Openexchangerates。我们只会创建助手、模型等文件夹中所示图像向右。
我们的扩展将有几个系统配置选项都可以在系统配置> >一般>货币,App Id和连接超时,所以我们的 system.xml看起来像这样:
<?xml version="1.0"?>
<!--
/**
* @category Magebase
* @package Magebase_Openexchangerates
* @copyright Copyright (c) 2014 Magebase
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
-->
<config>
<sections>
<currency translate="label" module="mb_openexchangerates">
<groups>
<mb_openexchangerates translate="label">
<label>Openexchangerates</label>
<sort_order>42</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
<fields>
<app_id translate="label comment">
<label>Openexchangerates App Id</label>
<frontend_type>text</frontend_type>
<sort_order>0</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
<comment><![CDATA[Get your App Id at <a href="http://openexchangerates.org">openexchangerates.org</a>]]></comment>
</app_id>
<timeout translate="label">
<label>Connection Timeout in Seconds</label>
<frontend_type>text</frontend_type>
<sort_order>5</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</timeout>
</fields>
</mb_openexchangerates>
</groups>
</currency>
</sections>
</config>
我们的模块 config.xml将简单:
<?xml version="1.0"?>
<!--
/**
* @category Magebase
* @package Magebase_Openexchangerates
* @copyright Copyright (c) 2014 Magebase
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
-->
<config>
<modules>
<Magebase_Openexchangerates>
<version>1.0.0</version>
</Magebase_Openexchangerates>
</modules>
<global>
<currency>
<import>
<services>
<openexchangerates>
<name>Openexchangerates</name>
<model>mb_openexchangerates/currency_import_openexchangerates</model>
</openexchangerates>
</services>
</import>
</currency>
<helpers>
<mb_openexchangerates>
<class>Magebase_Openexchangerates_Helper</class>
</mb_openexchangerates>
</helpers>
<models>
<mb_openexchangerates>
<class>Magebase_Openexchangerates_Model</class>
</mb_openexchangerates>
</models>
</global>
<default>
<currency>
<mb_openexchangerates>
<timeout>100</timeout>
</mb_openexchangerates>
</currency>
</default>
</config>
您将注意到在全球节点我们定义我们的新汇率线上购物服务。这个名字将出现在服务选择菜单和模型将是我们新的汇率类处理的检索和处理汇率从Openexchangerates的API。
的Openexchangerates API文档非常清楚和有很好的例子的各种用例和高级特性,但是,我们只会考虑免费提供的API调用,检索所有可用的利率在一个受到简单地调用 http://openexchangerates.org/api/latest.json?app_id=YOUR_APP_ID。我们得到数据为JSON所以很容易进一步解析和处理。
执行我们的检索,我们有 Model/Currency/Import/Openexchangerates.php与我们的类,它扩展了文件 Mage_Directory_Model_Currency_Import_Abstract:
<?php
/**
* @category Magebase
* @package Magebase_Openexchangerates
* @author Robert Popovic
* @copyright Copyright (c) 2014 Magebase (http://magebase.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Currency rate import model (From www.webservicex.net)
*
* @category Magebase
* @package Magebase_Openexchangerates
* @author Robert Popovic for Magebase
*/
class Magebase_Openexchangerates_Model_Currency_Import_Openexchangerates extends Mage_Directory_Model_Currency_Import_Abstract
{
/**
* Openexchangerates API URL
* @var string
*/
protected $_url = 'http://openexchangerates.org/api/latest.json?app_id={{APP_ID}}';
protected $_messages = array();
/**
* HTTP client
*
* @var Varien_Http_Client
*/
protected $_httpClient;
/**
* Fetched and cached rates array
* @var array
*/
protected $_rates;
/**
* Initialise our obkject with the full rates retrieved from Openexchangerates as the
* free version only allows us to get the complete set of rates. But that's ok, we are
* caching it and then processing the individual rates
*
* @throws Exception
*/
public function __construct()
{
$this->_httpClient = new Varien_Http_Client();
$app_id = Mage::getStoreConfig('currency/mb_openexchangerates/app_id');
if (!$app_id) {
$e = new Exception(Mage::helper('mb_openexchangerates')->__('No Openexchangerates App Id set!'));
Mage::logException($e);
throw $e;
}
$url = str_replace('{{APP_ID}}', $app_id, $this->_url);
$response = $this->_httpClient
->setUri($url)
->setConfig(array('timeout' => Mage::getStoreConfig('currency/mb_openexchangerates/timeout')))
->request('GET')
->getBody();
// response is in json format
if( !$response ) {
$this->_messages[] = Mage::helper('mb_openexchangerates')->__('Cannot retrieve rate from %s.', $url);
} else {
// check response content - returns array
$response = Zend_Json::decode($response);
if (array_key_exists('error', $response)) {
$this->_messages[] = Mage::helper('mb_openexchangerates')->__('API returned error %s: %s', $response['status'], $response['description']);
} elseif (array_key_exists('base', $response) && array_key_exists('rates', $response)) {
$this->_rates = $response['rates'];
} else {
Mage::log('Openexchangerates API request: %s',$url);
Mage::log('Openexchangerates API response: '.print_r($response,true));
$this->_messages[] = Mage::helper('mb_openexchangerates')->__('Unknown response from API, check system.log for details.');
}
}
}
/**
* Convert an individual rate
* Note that the Opexchangerates free service gives all rates based on USD
* so we do a cross-currency conversion vua USD as the base.
*
* @param string $currencyFrom
* @param string $currencyTo
* @param int $retry
* @return float
*/
protected function _convert($currencyFrom, $currencyTo, $retry=0)
{
if (sizeof($this->_messages) > 0) {
return null;
}
$rate = null;
if (array_key_exists($currencyFrom, $this->_rates) && array_key_exists($currencyTo, $this->_rates)) {
// convert via base currency, whatever it is.
$rate = (float) ($this->_rates[$currencyTo] * (1/$this->_rates[$currencyFrom]));
} else {
$this->_messages[] = Mage::helper('mb_openexchangerates')->__('Can\'t convert from '.$currencyFrom.' to '.$currencyTo.'. Rate doesn\'t exist.');
}
return $rate;
}
/**
* Trim currency rate to 6 decimals
*
* @param float $number
* @return float
*/
protected function _numberFormat($number)
{
return number_format($number, 6);
}
}
这个服务有两个特质相比旧服务是如何操作。首先,我们把所有利率在一个请求中,除非我们想支付溢价服务,我们可以调用服务通过特定货币之间的转换。第二,返回的利率都是基于汇率兑美元。这意味着我们不得不反思我们处理这个问题的方式。
首先,我们检索率在类构造函数的类被实例化时单击“导入率”按钮。然后我们只是缓存率数组中的JSON响应 _rates类属性。
线上购物然后穿过所有的货币和调用配置 _convert($currencyFrom, $currencyTo, $retry=0)方法为每个单独的货币。在这个阶段,我们的工作是容易的,我们只是查找货币代码数组中的键。我们唯一需要做的就是cross-convert以来,货币利率是基于美元,因此公式。
我们也有一个空的助手:
<?php
/**
* Helper class
*
* @category Magebase
* @package Magebase_Openexchangerates
* @author Robert Popovic for Magebase
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class Magebase_Openexchangerates_Helper_Data extends Mage_Core_Helper_Abstract
{
}
的最后一件事是应用程序/ etc /模块/ Magebase_Openexchangerates。xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Magebase_Openexchangerates>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Directory />
</depends>
</Magebase_Openexchangerates>
</modules>
</config>
还有你,享受持续自动为你的多种货币汇率更新网站!
(责任编辑:好模板) |