将Woocommerce商店转变为地理位置国家的目录?

问题描述 投票:2回答:1

我试图把我的woocommerce商店变成目录模式取决于country.I只想显示产品的价格和添加到购物车功能在美国。我试图通过在PHP中使用名为“GeoIP Detection”的插件和以下代码来实现这一目的:

/* Disable purchasing of products if the country is not United States*/
add_filter('woocommerce_is_purchasable', 'flatsome_woocommerce_is_purchasable', 10, 2);
function flatsome_woocommerce_is_purchasable($is_purchasable, $product) {
    $geoip = geoip_detect2_get_info_from_current_ip();
    $country = $geoip->raw[ 'country' ][ 'iso_code' ];
    if ( 'US' == $country ) { 
        return true;
        }
}
/*filter out prices based on country.*/
    add_filter( 'woocommerce_variable_sale_price_html', 'bdd_remove_prices', 10, 2 );
    add_filter( 'woocommerce_variable_price_html', 'bdd_remove_prices', 10, 2 );
    add_filter( 'woocommerce_get_price_html', 'bdd_remove_prices', 10, 2 );
function bdd_remove_prices( $price, $product ) {
    $geoip = geoip_detect2_get_info_from_current_ip();
    $country = $geoip->raw[ 'country' ][ 'iso_code' ];
    if ( 'US' !== $country )
    {$price = '';}
    return $price;
}

此代码确实有效,但没有像我预期的那样完全正常运行有时我的其他国家的朋友仍然可以看到价格,而且当我自己浏览网站时,我发现了一些奇怪的错误,想知道它是否只是缓存问题。

任何人都可以帮我改进这段代码吗?这对社区来说也非常有用。

我对这个编码也有一个奇怪的问题:

function flatsome_woocommerce_is_purchasable($is_purchasable, $product) {
    $geoip = geoip_detect2_get_info_from_current_ip();
    $country = $geoip->raw[ 'country' ][ 'iso_code' ];
    if ( 'US' == $country ) { 
        return true;
        }
}

我写的时候:

if ( 'US' !== $country ) { 
        return false;
        }

它不会起作用,所以我也很好奇为什么。

php wordpress woocommerce geolocation geoip
1个回答
2
投票

对于Woocommerce,您不需要使用任何插件

Woocommerce已经有专门的WC_Geolocation课程,可以做得更好。

使用Woocommerce WC_Geolocation类的代码:

// Utility function to get geolocated user country based on Woocommerce WC_Geolocation Class
function get_geolocated_user_country(){
   // 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 );

    return $user_geolocation['country'];
}


// Disable purchasing of products if the country is not United States
add_filter('woocommerce_is_purchasable', 'purchasable_country_based', 10, 2);
function purchasable_country_based( $is_purchasable, $product ) {
    // Enable for "US" only geolocated users country
    if( get_geolocated_user_country() ==='US' )
        return true;
    else
        return false;
}
// Filter out prices based on country
add_filter( 'woocommerce_variable_sale_price_html', 'display_prices_country_based', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'display_prices_country_based', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'display_prices_country_based', 10, 2 );
function display_prices_country_based( $price, $product ) {
    // Enable for "US" only geolocated users country
    if( get_geolocated_user_country() === 'US' )
        return $price;
    else
        return '';
}

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。

现在,当美国客户想要从其他国家购买时,如果他们正在旅行,你可能会遇到一些问题......

Woocommerce Geo IP相关答案:

© www.soinside.com 2019 - 2024. All rights reserved.