donglun4682 2018-05-29 03:21
浏览 78
已采纳

如何在wordpress中修复对未定义函数get_woocommerce_currency()的调用?

I'm developing a custom shipping plugin to my WooCommerce website.

I want to change shipping method to my custom method if and only if the user's currency is USD. Apart from USD, I'm using LKR as my other currency unit. If the currency is in LKR different shipping method should be applied.

So I tried to check whether the user's current base currency is in USD by using this code in my plugin.php file,

My plugin.php code includes in -> wp-content/plugins/my-plugin

 if ( 'USD' !== get_woocommerce_currency() ) {
    echo 'Hello  ';
    //my action
}   

So now every time I run this code I'm getting a fatal error saying

Call to undefined function get_woocommerce_currency()

Then I tried including the option.php ,

include_once('wp-includes/option.php');

still getting the same error message.

How to fix it and why I'm getting that error?

What I want to do is just to check user's current currency and activate the plugin if the currency is only in USD.

Thanks.

  • 写回答

2条回答 默认 最新

  • dongweiben5229 2018-05-29 03:30
    关注

    The fix for your issue will depend on how/when you're calling those lines of code you have.

    One is if you've added a hook on woocommerce_init this won't be a problem.

    A very very simple example to show what I mean.

    add_action( 'woocommerce_init', 'wc_init' );
    function wc_init(){
        // get_woocommerce_currency() will not throw error here.
    }
    

    If you're making a plugin that should work alongside with WooCommerce, I suggest you run your plugin init inside a function hook to woocommerce_init.


    I'm not really sure what you're up to. But if you're just interested in the currency, might as well just do this:

    $currency = apply_filters( 'woocommerce_currency', get_option( 'woocommerce_currency' ) );
    if ( 'USD' !== $currency ) {
        echo 'Hello  ';
        //my action
    }
    

    Again, working alongside with WooCommerce, you should use 'woocommerce_init' to hook your function.


    update 3

    function add_chalitha_shipping_method( $methods ) {
        if ( 'USD' == get_woocommerce_currency() ) {
             $methods[] = 'test_Shipping_Method';
        }
        return $methods;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题