Hiding prices based on visitor location backend bug in Woocommerce

一个人想着一个人 提交于 2019-12-19 11:25:54

问题


In my last question I asked how to hide prices to visitors outside the UK.

Based off of an answer I used this code successfully

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

This works as expected, however when I try to edit my products in the admin area I get this error in the price column of every product:

Fatal error: Uncaught Error: Call to a member function get_billing_country() on null in /var/sites/o/oxfordriderwear.com/public_html/wp-content/themes/storefront/functions.php:61 Stack trace: #0 /var/sites/o/oxfordriderwear.com/public_html/wp-includes/class-wp-hook.php(286): country_geolocated_based_hide_price('apply_filters('get_price_html() #4 /var/sites/o/oxfordriderwear.com/public_html/wp-content/plugins/woocommerce/includes/admin/list-tables/abstract-class-wc-admin-list-table.php(261): WC in /var/sites/o/oxfordriderwear.com/public_html/wp-content/themes/storefront/functions.php on line 61

Is there something incorrect in the code i've used, or something I need to add to fix this issue so that my admin area displays as normal?


回答1:


To avoid this problem we can return the formatted price on backend with the following:

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

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

No more bug.




回答2:


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 ) {
        $customer = WC_Customer(get_current_user_id());
        $country = $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;
}


来源:https://stackoverflow.com/questions/54924788/hiding-prices-based-on-visitor-location-backend-bug-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!