首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >wordpress函数中断wp-admin

wordpress函数中断wp-admin
EN

Stack Overflow用户
提问于 2017-04-14 20:30:50
回答 1查看 296关注 0票数 0

我已经在我的自定义WooCommerce站点中创建了一个函数。这在前端运行得很好,但是wp-admin中断了。Wp-admin显示http-500错误。

这是函数:

代码语言:javascript
复制
// 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,它将抛出以下消息:

代码语言:javascript
复制
Fatal error: Uncaught Error: Call to a member function get_shipping_country() on null in

所以它必须使用:$country = WC()->customer->get_shipping_country();但我找不到它。也许有人能帮我这个忙。

在进阶时谢谢。

EN

回答 1

Stack Overflow用户

发布于 2017-04-17 08:26:35

在后端中,customer属性未设置为WC_Customer的实例,因此您无法调用get_shipping_country()方法。

在使用customer之前,请检查它是否为空(默认)。

代码语言:javascript
复制
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' );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43411568

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档