1

In my functions.php I have some code inside a function that has a if (! is_admin) condition around it. For some reason the code inside this condition is running when I edit a page in the admin. Its throwing an error which results in the page not saving.

Here is a sample of the code setup.

function conditional_payment_gateways($available_gateways){

    if (!is_admin()){

        $payment_type_ins = false;
        $payment_type_both = false;
        
        foreach (WC()->cart->get_cart() as $cart_item){
            $payment_type = $cart_item['variation']['attribute_pa_payment-type'];
            $product_type = get_post_meta($cart_item['product_id'], 'type', true );
    
            if ($payment_type == 'pay-with-insurance' && $product_type != 'Both'){
                $payment_type_ins = true;
            }
    
            if ($product_type == 'Both'){
                $payment_type_both = true;
            }
        }
    
        if($payment_type_ins){
            unset($available_gateways['authorize_net_cim_credit_card']);
        } else if ($payment_type_ins == false && $payment_type_both == false){
            unset($available_gateways['cod']);
        }
    
        return $available_gateways;
    }
}

add_filter('woocommerce_available_payment_gateways', 'conditional_payment_gateways', 10, 1);

Here is the error it causes when it runs in the editor.

PHP Fatal error:  Uncaught Error: Call to a member function get_cart()

Is there a better way to do this?

Edit: Added the full code along with the error it causing when it runs in the editor

Edit #2:

OK, So I figured out a solution for my particular problem.

Instead of using !is_admin() I can use the is_cart() & is_checkout() conditionals.

function conditional_payment_gateways($available_gateways){

    if (is_cart() || is_checkout()){

        // Code here won't fire in the admin area
        
    }
}

add_filter('woocommerce_available_payment_gateways', 'conditional_payment_gateways', 10, 1);

But, I think the larger question still remains. If is_admin() is no longer reliable in the block editor, what should we use?

As Pat J pointed out in a answer below, this is supposed to be a work around. But it didn't work for me.

2
  • Why would it matter if code in the woocommerce_available_payment_gateways hook ran in the editor? That's a bit of a red flag. What code is running and what impact is it having? May 19 at 6:46
  • I edited the question to include the full code along with the error is causes when it runs in the editor
    – Jon
    May 19 at 13:17

1 Answer 1

0

It appears this is a known issue with the block editor (aka Gutenberg).

That Trac ticket suggests a workaround, too: replace your

if ( ! is_admin() ) { ... }

with something like this:

global $current_screen;
if ( 
    ( ! is_admin() ) && 
    ( ! 
        ( 
            // Checks the value of WP_Screen::is_block_editor().
            $current_screen instanceof \WP_Screen && 
            $current_screen->is_block_editor()
        )
    )
   ) { ... }

This code is untested. I'm pretty sure I matched the ( and ) but make sure it works as expected before you put it in production.

If you're doing this in a lot of locations (ie, > 1), I'd suggest creating a helper function even closer to the code suggested in the Trac ticket's comments:

function wpse416155_is_admin() {
    if ( is_admin() ) {
        return true;
    }
    global $current_screen;
    if ( $current_screen instanceof \WP_Screen &&
         $current_screen->is_block_editor() ) {
        return true;
    }
    return false;
}

// ...
// ...
// ...

if ( ! wpse416155_is_admin() ) {
    // ...your non-admin code here...
}
6
  • Interesting. I tested this and my code is still running in the editor. I tried changing the priority on add_filter as a desperate attempt but it had no effect.
    – Jon
    May 19 at 14:17
  • If I var_dump() $current_screen it returns like 4 times, the first is NULL and the rest have something like object(WP_Screen)#410 (18) { ... ["is_block_editor"]=> bool(true)}
    – Jon
    May 19 at 14:27
  • Hmm. It's possible that the woocommerce_available_payment_gateways hook happens too early for $current_screen to be set (see this comment in the Trac ticket), in which case I don't know where you'd go next.
    – Pat J
    May 19 at 14:27
  • 1
    Yeah, I seen that comment, that's why I was hoping changing the priority would do something but it didn't. Thanks for your time, I'll have to keep playing around
    – Jon
    May 19 at 14:30
  • 1
    I might have to do that, at least for now until I can find a better solution
    – Jon
    May 19 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.