在 WooCommerce 中禁用基于用户国家地理 IP 的所有付款方式

Posted

技术标签:

【中文标题】在 WooCommerce 中禁用基于用户国家地理 IP 的所有付款方式【英文标题】:Disable all payment methods based on user country geo-ip in WooCommerce 【发布时间】:2018-10-01 22:39:42 【问题描述】:

在我的 Woocommerce 商店中,我设置了地理定位系统,当地理定位识别除 IT 之外的任何国家/地区时,我想禁用付款方式

如果是 IT (geop-ip),请显示付款方式

如果是所有其他国家/地区 (geo-ip),请禁用所有付款方式。

【问题讨论】:

【参考方案1】:

Woocommerce 已经通过WC_Geolocation class 提供了地理定位 IP 功能,因此您不需要任何额外的插件。

这是禁用除“IT”(意大利)国家代码以外的所有国家/地区的支付网关的方法,具体取决于客户地理定位的 IP 国家/地区:

// Disabling payment gateways except for the defined country codes based on user IP geolocation country
add_filter( 'woocommerce_available_payment_gateways', 'geo_country_based_available_payment_gateways', 90, 1 );
function geo_country_based_available_payment_gateways( $available_gateways ) 
    // Not in backend (admin)
    if( is_admin() ) 
        return $available_gateways;

    // ==> HERE define your country codes
    $allowed_country_codes = array('IT');

    // Get an instance of the WC_Geolocation object class
    $geolocation_instance = new WC_Geolocation();
    // Get user IP
    $user_ip_address = $geolocation_instance->get_ip_address();
    // Get geolocated user IP country code.
    $user_geolocation = $geolocation_instance->geolocate_ip( $user_ip_address );

    // Disable payment gateways for all countries except the allowed defined coutries
    if ( ! in_array( $user_geolocation['country'], $allowed_country_codes ) )
        $available_gateways = array();

    return $available_gateways;

代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。


相关:

Disable WooCommerce payment gateway for guests and specific user roles Enabling Payment method based on the customers location Hide specific payment method based on total weight in Woocommerce Hide payment method based on product type in WooCommerce

【讨论】:

我将如何禁用特定的支付网关? @JulianWagner 例如使用 unset($available_gateways['bacs']); 而不是 $available_gateways = array(); 谢谢你,我自己发现了,并在下面发布了一个更改的 sn-p。【参考方案2】:

这是@LoicTheAztec 答案的变体,它仅禁用特定的付款方式而不是全部:

add_filter( 'woocommerce_available_payment_gateways', 'geo_country_based_available_payment_gateways', 90, 1 );
function geo_country_based_available_payment_gateways( $available_gateways ) 
  // Not in backend (admin)
  if( is_admin() )
    return $available_gateways;

  // ==> HERE define your country codes
  $allowed_country_codes = array('DE','AT');

  // Get an instance of the WC_Geolocation object class
  $geolocation_instance = new WC_Geolocation();
  // Get user IP
  $user_ip_address = $geolocation_instance->get_ip_address();
  // Get geolocated user IP country code.
  $user_geolocation = $geolocation_instance->geolocate_ip( $user_ip_address );

  // Disable payment gateways for all countries except the allowed defined coutries
  if ( ! in_array( $user_geolocation['country'], $allowed_country_codes ) ) 
    unset( $available_gateways['stripe_sofort'] );
  

  return $available_gateways;

【讨论】:

【参考方案3】:

我知道 Istack,以及 maxmind 等 .. 我想到了像这个函数这样更简单的东西,它基于 blling_country 而不是 geo-ip 国家:

function payment_gateway_disable_country( $available_gateways ) 
global $woocommerce;
if ( is_admin() ) return;
if ( isset( $available_gateways['authorize'] ) && $woocommerce->customer->get_billing_country() <> 'US' ) 
unset( $available_gateways['authorize'] );
 else if ( isset( $available_gateways['paypal'] ) && $woocommerce->customer->get_billing_country() == 'US' ) 
unset( $available_gateways['paypal'] );

return $available_gateways;


add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );

【讨论】:

【参考方案4】:

要找出用户所在的国家/地区,您可以使用FreeGeoIp, now renamed to Ipstack 之类的工具。您为服务提供一个 IP 地址,它会告诉您用户可能所在的国家/地区地址(以及其他信息)。

有两种选择 1. 使用他们托管的 API(10,000 个请求免费,超过 10,000 个请求付费) 2. 从 GitHub 链接下载版本并自己托管

当您需要知道用户所在的国家/地区时,您可以向 API 发送带有用户 IP 地址的 HTTP 请求,然后使用该信息来启用或禁用付款方式。

【讨论】:

以上是关于在 WooCommerce 中禁用基于用户国家地理 IP 的所有付款方式的主要内容,如果未能解决你的问题,请参考以下文章

在 WooCommerce 中禁用基于自定义运输方式的付款方式

在 Woocommerce 3 中获取用户地理定位的国家/地区名称

WooCommerce 中多个国家/地区的基于地理位置的自定义重定向

避免访问基于 WooCommerce 地理定位国家/地区的特定产品

不允许客户在 WooCommerce 结帐中更改国家/地区

WooCommerce 中特定产品的基于地理位置的自定义重定向