How to fix Call to undefined function get_woocommerce_currency() in wordpress?

Old Ceylon Mudliar

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.

Reigel

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;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to fix "undefined is not a function" in WordPress

Call for undefined function in Wordpress plugin

Call to undefined method; how to fix?

Wordpress Fatal error: Call to undefined function get_header()

Wordpress - PHP Fatal error: Call to undefined function get_bloginfo()

Call to undefined function get_field_id() in wordpress

How to call a function in wordpress?

Wordpress/PHP call to undefined function error

How to fix error Call to undefined function link_to_route() laravel 5?

Woocommerce - How to fix Uncaught Error: Call to a member function get_image() on bool in

How to fix "Uncaught Error: Call to a member function get_tax_class() on null" in WooCommerce?

WordPress Plugin: How to resolve "Call to undefined function add_action() as well as add_action()"?

How to fix Uncaught TypeError: Cannot call method 'call' of undefined in fullcalendar

How to fix: undefined reference to "class::function"

How to fix 'Typerror is undefined' message for a function in Javascript?

How to fix undefined is not a function listing the state in the render

How to fix async function which returns `undefined`?

How to fix "undefined method" error in destroy function

How to fix Call to a member function where() on int

How to fix this error no matching function for call?

Wordpress : Call to undefined function register_new_user()

Uncaught Error: Call to undefined function mysql_connect() - WordPress Setup

Wordpress theme development : Call to undefined function WP_Query()

Call to undefined function wp_pagenavi() Error in Wordpress pagination

Showing fatal error call to undefined function _e() in wordpress

How to fix call_user_func_array() expects parameter 1 to be a valid callback, function 'my_cpt_single_template' in Wordpress

How to fix 'Uncaught Error: Call to undefined method' in PHP

How to fix Call to undefined method Maatwebsite\Excel\Excel::create()?

How to Fix "Attempted to call an undefined method named "createNamedBuilder..."?