根据用户位置隐藏WooCommerce产品详细信息

时间:2019-03-01 13:11:12

标签: wordpress woocommerce globalization

我已经使用woocommerce开发了目录(非购买产品),但是由于无法控制的原因,我需要能够对来自英国以外访问该网站的用户隐藏产品价格。

我正在使用Hiding prices based on visitor location backend bug in Woocommerce (我最后一个问题的答案)

add_filter( 'woocommerce_get_price_html', 'country_geolocated_based_hide_price', 10, 2 );
function country_geolocated_based_hide_price( $price, $product ) {
    // Not on backend
    if( is_admin() ) 
        return $price;

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

我一直在使用以下方法进行测试:

在大多数情况下(根据这些网站),这似乎是可行的,但是我注意到结果有些不一致。某些页面似乎仍然显示产品价格。 我担心我的插件或自定义CSS之一可能会造成干扰,但是由于我只能访问这些网站进行测试,因此我不确定。

网站为:https://oxfordriderwear.com/product-category/mens/

是否有任何人可以发现可能导致此问题的原因/知道找出答案的方法?还是有更可靠的测试方法?

0 个答案:

没有答案
相关问题