您可能要禁用非本地人的 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,並在您的結帳頁面進行一些測試!

而已!


可以在哪裏添加此代碼?

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


這段代碼是否正常可用?

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


需要關於 WooCommerce 的幫助?

請觀看我們提供的免費視頻教程或到薇曉朵 WooCommerce 中文論壇提問,會有專業技術團隊提供相關幫助。