在 Woocommerce 中仅显示特定客户所在国家/地区的价格

2021-12-22 00:00:00 geolocation php wordpress woocommerce price

我已经使用 woocommerce 开发了一个目录,但是由于我无法控制的原因,我需要能够对从英国以外访问该网站的用户隐藏产品价格.

我找到了允许我根据访问者位置更改产品价格的插件,但没有任何东西可以让我隐藏价格.

是否有我遗漏的插件或我可以添加到 woocommerce 文件中以实现此目的的任何内容?

解决方案

以下内容将隐藏英国以外的价格,基于客户地理定位的国家:

add_filter( 'woocommerce_get_price_html', 'country_geolocated_based_hide_price', 10, 2 );函数 country_geolocated_based_hide_price( $price, $product ) {//获取 WC_Geolocation 对象类的实例$geo_instance = new WC_Geolocation();//获取地理定位的用户地理数据.$user_geodata = $geo_instance->geolocate_ip();//获取当前用户的 GeoIP 国家$country = $user_geodata['country'];返回 $country !== 'GB' ?'' : $价格;}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

<小时>

如果您只想为未登录的客户启用该地理定位功能,请使用以下命令:

add_filter( 'woocommerce_get_price_html', 'country_geolocated_based_hide_price', 10, 2 );函数 country_geolocated_based_hide_price( $price, $product ) {如果(get_current_user_id()> 0){$country = WC()->customer->get_billing_country();} 别的 {//获取 WC_Geolocation 对象类的实例$geo_instance = new WC_Geolocation();//获取地理定位的用户地理数据.$user_geodata = $geo_instance->geolocate_ip();//获取当前用户的 GeoIP 国家$country = $user_geodata['country'];}返回 $country !== 'GB' ?'' : $价格;}

<块引用>

此代码的更新版本在此答案上可用,避免了后端错误.

我在开始时添加了函数:

if ( is admin() ) return $price;

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

I've developed a catalogue using woocommerce, however I need to be able to hide product prices from users who are visiting the site from outside of the UK due to reasons outside of my control.

I've found plugins that allow me to change product prices based on visitor location, but nothing allows me to hide the prices.

Is there any plugins that I've missed or anything I can add in to the woocommerce files to achieve this?

解决方案

The following will hide prices outside United kingdom based on customer geolocated country:

add_filter( 'woocommerce_get_price_html', 'country_geolocated_based_hide_price', 10, 2 );
function country_geolocated_based_hide_price( $price, $product ) {
    // Get an instance of the WC_Geolocation object class
    $geo_instance  = new WC_Geolocation();
    // Get geolocated user geo data.
    $user_geodata = $geo_instance->geolocate_ip();
    // Get current user GeoIP Country
    $country = $user_geodata['country'];

    return $country !== 'GB' ? '' : $price;
}

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


If you want to enable that geolocated feature only for unlogged customers, use the following:

add_filter( 'woocommerce_get_price_html', 'country_geolocated_based_hide_price', 10, 2 );
function country_geolocated_based_hide_price( $price, $product ) {
    if( get_current_user_id() > 0 ) {
        $country = WC()->customer->get_billing_country();
    } else {
        // Get an instance of the WC_Geolocation object class
        $geo_instance  = new WC_Geolocation();
        // Get geolocated user geo data.
        $user_geodata = $geo_instance->geolocate_ip();
        // Get current user GeoIP Country
        $country = $user_geodata['country'];
    }
    return $country !== 'GB' ? '' : $price;
}

An updated version of this code is available on this answer avoiding a backend bug.

I have added in the function on start:

if ( is admin() ) return $price;

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

相关文章