禁用WooCommerce中除基于Geo-IP国家/地区的BAC之外的所有支付网关 [英] Disable all payment gateways except BACS based on geo-ip country in Woocommerce

查看:0
本文介绍了禁用WooCommerce中除基于Geo-IP国家/地区的BAC之外的所有支付网关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WooCommerce中,我使用的代码来自this answer thread,如果用户的GEO IP来自一组允许的国家/地区,则启用所有支付网关。此处我需要的允许国家/地区代码是"SE"(瑞典)

我想要的是,如果GEO IP不在瑞典(预定义的允许国家/地区),则禁用除BAC之外的所有支付网关。

感谢任何帮助。

推荐答案

以下代码将禁用除不允许的GEO IP定义的国家/地区(此处为瑞典)的所有可用支付网关

// Disabling payment gateways (except BACS) 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('SE');

    // 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 (except BACS) for all countries except the allowed defined countries
    if ( ! in_array( $user_geolocation['country'], $allowed_country_codes ) ){
        $bacs_gateways              = $available_gateways['bacs'];
        $available_gateways         = array();
        $available_gateways['bacs'] = $bacs_gateways;
    }

    return $available_gateways;
}

代码放在活动子主题(或活动主题)的函数.php文件中。已测试并正常工作。

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

这篇关于禁用WooCommerce中除基于Geo-IP国家/地区的BAC之外的所有支付网关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆