简体   繁体   English

wordpress功能中断wp-admin

[英]wordpress function breaks wp-admin

I've created a function in my custom WooCommerce site. 我已经在自定义的WooCommerce网站中创建了一个函数。 This works great on the frontend, but wp-admin breaks. 这在前端很有效,但是wp-admin中断了。 Wp-admin shows a http-500 error. WP-admin显示一个http-500错误。

This is the function: 这是功能:

// 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');

I've set the wp-debug on true and it will throw this message: 我将wp-debug设置为true,它将抛出以下消息:

Fatal error: Uncaught Error: Call to a member function get_shipping_country() on null in

So it has to do something with: $country = WC()->customer->get_shipping_country(); 因此,它必须执行以下操作:$ country = WC()-> customer-> get_shipping_country(); but I can't find it. 但我找不到 Maybe someone can help me with this. 也许有人可以帮助我。

Thanks in advanced. 提前致谢。

The customer property isn't set to an instance of WC_Customer in the backend so you can't call the get_shipping_country() method. customer属性未在后端设置为WC_Customer的实例,因此您无法调用get_shipping_country()方法。

Check customer isn't null (default) before using it. 使用customer之前,请先检查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' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM