Disable payment gateways based on user country geo-ip in Woocommerce

后端 未结 3 2029
被撕碎了的回忆
被撕碎了的回忆 2020-12-20 08:25

In my Woocommerce shop I set up the geolocation system, when geolocation identifies any country other than IT I would like to disable payment methods

If it is IT (ge

相关标签:
3条回答
  • 2020-12-20 09:07

    Woocommerce has already a geolocation Ip feature through WC_Geolocation class, so you don't need any additional plugin.

    Here is the way to disable payment gateways for all countries except "IT" (Italy) country code, based on costumer geolocated IP country:

    // 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;
    }
    

    Code goes in functions.php file of your active child theme (or active theme). Tested and works.

    0 讨论(0)
  • 2020-12-20 09:24

    I know Istack, as well as maxmind etc .. I thought something simpler like this function, which is based on the blling_country and not on the geo-ip country:

    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' );
    
    0 讨论(0)
  • 2020-12-20 09:25

    In order to find out the user's country you can use a tool like FreeGeoIp, now renamed to Ipstack. You provide the service an IP address and it will tell you the country address the user is likely in (among other information).

    There are two options 1. Using their hosted API (free for 10,000 requests and paid for more than that) 2. Downloading a release from the GitHub link and hosting it yourself

    When you need to know the user's country you can send a HTTP request with the user's IP address to the API and then use that information to enable or disable the payment method.

    0 讨论(0)
提交回复
热议问题