如果你使用 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