使用ECShop搭建外贸站多国货币切换功能

时间:2022-08-31 15:20:20
//1   在数据库里的表ecs_shop_config插入


insert into ecs_shop_config('id','parent_id','code','type','store_range','store_dir','value','sort_order') values(null,'1','rate','text','','','1,0.71,0.69,6.85,1.45','1'),(null,'1','ybprice_format','text','','','&%s','1'),(null,'1','aprice_format','text','','','EUR%s','1'),(null,'1','cprice_format','text','','','¥%s','1'),(null,'1','aoprice_format','text','','','AU%s','1');
//INSERT INTO `ecs_shop_config`(`id`, `parent_id`, `code`, `type`, `store_range`, `store_dir`, `value`, `sort_order`) VALUES (null,'1','rate','text','','','1,0.71,0.69,6.85,1.45','1'),(null,'1','ybprice_format','text','','','&%s','1'),(null,'1','aprice_format','text','','','EUR%s','1'),(null,'1','cprice_format','text','','','¥%s','1'),(null,'1','aoprice_format','text','','','AU%s','1')




//2   在/languages/zh_cn/admin/shop_config.php下添加 (73行)
 
 $_LANG['cfg_name']['rate'] = '货币汇率';
 $_LANG['cfg_name']['ybprice_format'] = '英镑格式';
 $_LANG['cfg_name']['aprice_format'] = '欧元格式';
 $_LANG['cfg_name']['cprice_format'] = '人民币格式';
 $_LANG['cfg_name']['aoprice_format'] = '澳元格式';


//并且添加下面的帮助信息 (199行)


$_LANG['cfg_desc']['rate'] = '输入规则按照和美元的汇率进行出入 Us,EURP,BriishPound,China,Austriliar';
$_LANG['cfg_desc']['ybprice_format'] = '显示英镑格式,%s将被替换为相应的价格。';
$_LANG['cfg_desc']['aprice_format'] = '显示欧元格式,%s将被替换为相应的价格。';
$_LANG['cfg_desc']['cprice_format'] = '显示人民币格式,%s将被替换为相应的价格。';
$_LANG['cfg_desc']['aoprice_format'] = '显示澳元格式,%s将被替换为相应的价格。';








//3.在themes/当前使用的模板文件夹/library/page_header.lbi的合适位置添加


<div class="TopNavList">
<li><a href="{$url_head}&currency=USD">美元</a><span></span></li>
<li><a href="{$url_head}&currency=CNY">人民币</a><span></span></li>
<li><a href="{$url_head}&currency=EUR">欧元</a><span></span></li>
<li><a href="{$url_head}&currency=GBP">英镑</a><span></span></li>
<li><a href="{$url_head}&currency=AUD">澳元</a><span></span></li>
</div> 
<select>
<option value="{$url_head}&currency=USD">美元</option>
<option value="{$url_head}&currency=CNY">人民币</option>
<option value="{$url_head}&currency=EUR">欧元</option>
<option value="{$url_head}&currency=GBP">英镑</option>
<option value="{$url_head}&currency=AUD">澳元</option>
</select>






//或
<div class="TopNavList">
<ul>
<li><a href="{$url_head}&currency=USD">美元</a><span></span></li>
<li><a href="{$url_head}&currency=CNY">人民币</a><span></span></li>
<li><a href="{$url_head}&currency=EUR">欧元</a><span></span></li>
<li><a href="{$url_head}&currency=GBP">英镑</a><span></span></li>
<li><a href="{$url_head}&currency=AUD">澳元</a><span></span></li>
</ul>
</div>








//    4在/includes/init.php最后面添加 //路径处理 


$url_this="http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?id=".@$_GET['id'];
$smarty->assign("url_head",$url_this);

$currency=@$_GET['currency'];

clear_all_files(); 清除缓存功能

if($currency!=""){
$_SESSION['currency']=$currency;
}
if($_SESSION['currency']==''){
$_SESSION['currency']='USD';
}






//    5.在/includes/lib_commom.php找到这个函数price_format并且按下面的进行修改 
/**   * 格式化商品价格 
 *   * @access  public 
 * @param   float   $price  商品价格 
 * @return  string 
*/


function price_format($price,$change_price=true){
$currency=$_SESSION['currency'];
$rate=explode(',',$GLOBALS['_CFG']['rate']);
if($currency=='USD'){
$price=$price*$rate[0];
}
if($currency=='CNY'){
$price=$price*$rate[3];
}
if($currency=='EUR'){
$price=$price*$rate[1];
}
if($currency=='GBP'){
$price=$price*$rate[2];
}
if($currency=='AUD'){
$price=$price*$rate[4];
}
if($change_price && defined('ESC_ADMIN')==false){
switch($GLOBALS['_CFG']['price_format']){
case 0:
$price=number_format($price,2,'.','');
break;
case 1: //保留不为3的尾数
$price=preg_replace('/(.*)(\\.)([0-9]*?)0+$/','\1\2\3',number_format($price,2,'.',''));
if(substr($price,-1)=='.'){
$price=substr($price,0,-1);
}
break;
case 2: //四舍五入,保留1位
$price=substr(number_format($price,2,'.',''),0,-1);
break;
case 3: //直接取整
$price=intval($price);
break;
case 4: //四舍五入,保留1位
$price=number_format($price,1,'.','');
break;
case 5: //四舍五入,不保留小数
$price=round($price);
break;
}
}else{
$price=number_format($price,2,'.','');
}
switch ($currency){
case 'USD':
return sprintf($GLOBALS['_CFG']['currency_format'],$price);
break;
case 'CNY':
return sprintf($GLOBALS['_CFG']['cprice_format'],$price);
break;
case 'EUR':
return sprintf($GLOBALS['_CFG']['aprice_format'],$price);
break;
case 'GBP':
return sprintf($GLOBALS['_CFG']['ybprice_format'],$price);
break;
case 'AUD':
return sprintf($GLOBALS['_CFG']['aoprice_format'],$price);
break;
}
}






// 6.修改表ecs_order_info


alter table 'ecs_order_info' add 'currency' varchar(10) not null,add 'new_money' decimal(10,2) not null








// 7.修改flow.php文件中
{
//分成功能关闭
$parent_id=0;
}


$order['parent_id']=$parent_id;


//大约1608行左右下面  插入以下代码


$order['currency']=$_SESSION['currency'];
$order['new_money']=price_format_hs($order['order_amount']);


//同时修改\inlucdes\lib_common.php在里面新增加price_format_hs函数


/** 
  * 用于支付换算  * 
 * @access  public 
 * @param   float   $price  商品价格  * @return  string 
*/


function price_format_hs($price,$change_price=true){
$currency=$_SESSION['currency'];
$rate=explode(',',$GLOBALS['_CFG']['rate']);
if($currency=='USD'){
$price=$price*$rate[0];
}
if($currency=='CNY'){
$price=$price*$rate[3];
}
if($currency=='EUR'){
$price=$price*$rate[1];
}
if($currency=='GBP'){
$price=$price*$rate[2];
}
if($currency=='AUD'){
$price=$price*$rate[4];
}
if($change_price && defined('ESC_ADMIN')==false){
switch ($GLOBALS['_CFG']['price_format']){
case 0:
$price=number_format($price,2,'.','');
break;
case 1: //保留不为0的尾数
$price=price_replace('/(.*)(\\.)([0-9]*?)0+$/','\1\2\3',number_format($price,2,'.',''));
if(substr($price,-1)=='.'){
$price=substr($price,0,-1);
}
break;
case 2: //不四舍五入,保留1位
$price=substr(number_format($price,2,'.',''),0,-1);
break;
case 3: //直接取整
$price=intval($price);
break;
case 4: //四舍五入,保留1位
$price=number_format($price,1,'.','');
break;
case 5: //四舍五入,不保留小数
$price=round($price);
break;
}
}else{
$price=number_format($price,2,'.','');
}
return $price;
}






// 8.在\includes\modules\payment\paypal.php 大约92行一个get_code函数


function get_code($order,$payment){
$paypal_currency=$_SESSION["currency"]; //新增加的
$data_order_id=$order['log_id'];
$data_amount=$order['order_amount'];
$data_return_url=return_url(basename(__FILE__,'.php'));
$data_pay_account=$payment['paypal_account'];
$currency_code=$paypal_currency; //把下一行复制出来,并下行注掉,修改为这一行
//$currency_code=$payment['paypal_currency'];
}