您可能要禁用非本地人的 PayPal,或僅為特定國家啟用特定閘道器。對於國際貿易的所有人來說,這是一個非常普遍的要求。

如何禁用/啟用特定國家/地區的特定閘道器
- 決定要禁用/啟用的支付閘道器,例如 “paypal”,“授權”,“條紋” 等。
- 確定國家程式碼,例如美國,ES,IE 等
- 開啟 WordPress 儀表板/外觀/ Editor / functions.php 檔案
- 新增以下程式碼行:
/** * @snippet WooCommerce Disable Payment Gateway for a Specific Country * @sourcecode https://businessbloomer.com/?p=164 * @author Rodolfo Melogli * @compatible WooCommerce 2.4.7 */ function payment_gateway_disable_country( $available_gateways ) { global $woocommerce; if ( isset( $available_gateways['authorize'] ) && $woocommerce->customer->get_country() <> 'US' ) { unset( $available_gateways['authorize'] ); } else if ( isset( $available_gateways['paypal'] ) && $woocommerce->customer->get_country() == 'US' ) { unset( $available_gateways['paypal'] ); } return $available_gateways; } add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );
功能細節
給出上面的例子,這裡是您需要更改以使其工作的所有內容:
1)為您的函式決定一個名字,以便您知道這個函式是什麼。在我的例子中,我呼叫函式 “payment_gateway_disable_country”,因為它是一個翻譯 “Leave a Reply” 字串的函式。確保此名稱出現在第 1 行
2)在第一個 “if” 宣告中,我決定在美國境外取消 “授權” 付款。您需要做的是在此輸入付款名稱:
- if(isset($ available_gateways [‘authorize’])
- unset($ available_gateways [‘authorize’]);
… 和國家程式碼在這裡:
- && $ woocommerce-> customer-> get_country()<>’US’
3)在 “else if” 語句中,如果該國是美國,我決定停用 PayPal 。根據 2 輸入相同的資訊)
4)儲存您的 functions.php,並在您的結帳頁面進行一些測試!
而已!
可以在哪裡新增此程式碼?
您可以將 PHP 程式碼片段放置在主題或子主題的 functions.php 檔案的底部(如果是 CSS 程式碼,請新增到主題的 style.css 檔案底部),修改之前建議先備份原始檔案,若出現錯誤請先刪除此程式碼。
這段程式碼是否正常可用?
或者是您有更好的解決方案想要分享?請到薇曉朵 WooCommerce 中文論壇留言告知,我們希望可以幫到更多國內的 WooCommerce 使用者也希望您的參與。
需要關於 WooCommerce 的幫助?
請觀看我們提供的免費影片教程或到薇曉朵 WooCommerce 中文論壇提問,會有專業技術團隊提供相關幫助。