让WooCommerce 中文网关支持PayPal 并自动按汇率进行转换

让WooCommerce 中文网关支持PayPal 并自动按汇率进行转换

如果你使用WooCommerce 并将本地设置为中国,那么如果启用PayPal 支付方式,会提示你:贝宝不支持你的商铺货币。本文就是解决这个问题,让WooCommerce 中文网关支持PayPal 并自动按汇率进行转换。

非PayPal 支持区域的WooCommerce 用户会遇到“ 网关已禁用: 贝宝不支持你的商铺货币。”的提示(Gateway Disabled: PayPal does not support your store's currency. )。如下图:

与这一设定有关的函数是woocommerce_paypal_supported_currencies,hook之:

add_filter( 'woocommerce_paypal_supported_currencies', 'enable_custom_currency' );
function enable_custom_currency($currency_array) {
  $currency_array[] = 'CNY';
  return $currency_array;
}

加上上面这段就可以让中文网关也支持PayPal 了。不过当你PayPal 设置好后,你会发现在人民币的价格币种下,使用PayPal 支付时候会直接转化为美元而已——即如果说商品售价¥99,那么用paypal支付就是支付 $99 ——这种情况你的顾客肯定不满意。下面的代码就可以让PayPal 支付时候将人民币数额转化为相应的美元数额(代码来自solagirl)。

//美元人民币转,汇率自己定义
add_filter('woocommerce_paypal_args', 'convert_rmb_to_usd');
function convert_rmb_to_usd($paypal_args){
    if ( $paypal_args['currency_code'] == 'CNY'){
        $convert_rate = 6.2; //Set converting rate
        $count = 1;
        while( isset($paypal_args['amount_' . $count]) ){
            $paypal_args['amount_' . $count] = round( $paypal_args['amount_' . $count] / $convert_rate, 2);
            $count++;
        }       
    }
    return $paypal_args;
}

代码已经托管到Github gist,查看:https://gist.github.com/Jeff2Ma/91f148fc301a8ae0851b

评分:
当前平均分 0.00 (0%) - 0 个投票
3 条 评论
  1. 那装WooCommerce的时候,把本地设置成国外就可以没有这问题吗? 反正我们的外贸目标是卖给国外的买家。

    5年前 回复
  2. 可以 了 谢谢

    8年前 回复
  3. 没用啊》。。 :sad:

    8年前 回复
发表评论