根据 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 ) {
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;
}
这按预期工作,但是当我尝试在管理区域编辑我的产品时,我在每个产品的价格栏中收到此错误:
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
我使用的代码有什么不正确的地方吗?或者我需要添加一些东西来解决这个问题,以便我的管理区域显示正常?
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;
}
为避免此问题,我们可以 return 后端的格式化价格,如下所示:
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;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
没有更多错误。
在上一个问题中,我询问了如何向英国境外的访客隐藏价格。
根据一个答案,我成功地使用了这段代码
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;
}
这按预期工作,但是当我尝试在管理区域编辑我的产品时,我在每个产品的价格栏中收到此错误:
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
我使用的代码有什么不正确的地方吗?或者我需要添加一些东西来解决这个问题,以便我的管理区域显示正常?
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;
}
为避免此问题,我们可以 return 后端的格式化价格,如下所示:
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;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
没有更多错误。