1

I've built a plugin and I have the following issue: The WooCommerce Dashboard (in the admin side) will not load the data. It hangs and fails. I have tracked the problem code:

The issue in the

if ( is_admin() ) { 
    //removed   
} else if ( !$this->is_login_page() && !wp_doing_ajax() ) {

    $public = new Public();
}

It's the public side code that's causing the issue! and neither is_admin or wp_doing_ajax prevent it from happening.

In the public side, I'm calling

add_action( 'init', array('Dynamic_Rules', 'dynamic_rule_tax_exemption') );

Inside the tax exemption function, I have this code in particular which causes the problem:

$woocommerce = WC();

$user_country = $woocommerce->customer->get_billing_country();

$woocommerce->customer->set_is_vat_exempt(true);

So I can only speculate about what happens, perhaps the WC() is somehow sending everything into an infinite loop, which is why the Dashboard does not load the data. Why is_admin() and wp_doing_ajax() don't prevent this from happening I don't know.

Perhaps it's wrong that I'm calling that function on init, but where else could I call it?

Any help is appreciated

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Stefan S
  • 23
  • 4

1 Answers1

1

Is difficult to find out what your problem can be… Note that "your question should be updated to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem."

What you can try instead, may be:

$customer = WC()->customer;

if( ! is_a( $customer, 'WC_Customer' ) {
    global $current_user;

    if( $current_user > 0 ) { 
        $customer = new WC_Customer( $current_user->ID );
    }
}

if( is_a( $customer, 'WC_Customer' ) {
    $billing_country = $customer->get_billing_country();

    if( ! $customer->is_vat_exempt() ) {
        $customer->set_is_vat_exempt( true );
    }   
} else {
    // Some code to throw an error or debug trace
}

I hope this will solve your issue. If not you need to pass the User ID to your code in some way.

Maybe useful: Debugging WooCommerce PHP with Javascript console.log doesn't work

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks a lot brother, that check, is_a($customer, 'WC_Customer') did the trick. Hey, would you mind taking a look at this question too? https://stackoverflow.com/questions/61958431/problem-running-new-wp-query-inside-pre-get-posts – Stefan S May 29 '20 at 09:34
  • did it now, tried to before but didn't have enough reputation. – Stefan S May 29 '20 at 11:22