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

时间:2022-04-23
本文章向大家介绍让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