doo58088 2018-04-21 10:21
浏览 311
已采纳

在Woocommerce中根据用户国家/地区地理位置禁用支付网关

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 (geop-ip), show payment.

If all other country (geo-ip), disable payment.

  • 写回答

3条回答 默认 最新

  • dongwei7048 2018-04-21 12:32
    关注

    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 ) {
        if ( is_admin() ) return; // Only on front end
    
        // ==> 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 function.php file of your active child theme (or active theme). Tested and works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探