如何在 WooCommerce 中禁用特定國家的付款網關

如何在 WooCommerce 中禁用特定國家的付款網關

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

如何在 WooCommerce 中找到 Gateway ID

如何禁用/啟用特定國家/地區的特定網關

  • 決定要禁用/啟用的支付網關,例如 “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,並在您的結帳頁面進行一些測試!

而已!

如何添加此代碼?

1、您可以將 PHP 代碼片段放置在主題或子主題的 functions.php 文件的底部(如果是 CSS 代碼,請添加到子主題的 style.css 文件底部)修改之前建議先備份原始文件,若出現錯誤請先刪除此代碼。

2、WordPress 4.9 後改進了主題編輯器,對於 CSS 代碼也可打開網站前台編輯器的【自定義】,複製代碼添加到自定義 css 中。

此代碼是否可用?

如需幫助或是您有更好的方案想分享?請到薇曉朵 WooCommerce 中文論壇留言告知,我們希望可以幫到更多國內的 WooCommerce 用戶也希望您的參與。

文章沒看懂?代碼不會用?需要幫助您可以去論壇提問自助服務台

作者小新

大象、大象 ,你的鼻子怎麼那麼長 ,媽媽說鼻子長才是漂亮 ......