我已经在我的自定义WooCommerce站点中创建了一个函数。这在前端运行得很好,但是wp-admin中断了。Wp-admin显示http-500错误。
这是函数:
// Set currency based on visitor country
function geo_client_currency($client_currency) {
$country = WC()->customer->get_shipping_country();
switch ($country) {
case 'GB': return 'GBP'; break;
default: return 'EUR'; break;
}
}
add_filter('wcml_client_currency','geo_client_currency');我已经将wp-debug设置为true,它将抛出以下消息:
Fatal error: Uncaught Error: Call to a member function get_shipping_country() on null in所以它必须使用:$country = WC()->customer->get_shipping_country();但我找不到它。也许有人能帮我这个忙。
在进阶时谢谢。
发布于 2017-04-17 08:26:35
在后端中,customer属性未设置为WC_Customer的实例,因此您无法调用get_shipping_country()方法。
在使用customer之前,请检查它是否为空(默认)。
function geo_client_currency( $client_currency ) {
if ( WC()->customer ) {
$country = WC()->customer->get_shipping_country();
/**
* Assuming more are going to be added otherwise a switch is overkill.
* Short example: $client_currency = ( 'GB' === $country ) ? 'GBP' : 'EUR';
*/
switch ( $country ) {
case 'GB':
$client_currency = 'GBP';
break;
default:
$client_currency = 'EUR';
}
}
return $client_currency;
}
add_filter( 'wcml_client_currency', 'geo_client_currency' );https://stackoverflow.com/questions/43411568
复制相似问题