0

I am using this plugin to switch users. I have found a snippet that allows you to switch user on the WooCommerce admin order page which is very helpful.

add_action( 'woocommerce_admin_order_data_after_order_details', 'ccef_order_switch_to', 99 );
/**
 * Add a switch to user link on the order details.
 *
 * @param object $order The WC Order that we're working with.
 */
function ccef_order_switch_to( $order ) {
    $user_switching = $GLOBALS['user_switching'];
    if ( $user_switching ) {
        $user        = get_user_by( 'id', $order->get_user_id() );
        $switch_link = $user_switching::maybe_switch_url( $user );
        if ( $switch_link ) {
            echo '<a href="' . esc_url( $switch_link ) . '">Switch To</a>';
        }
    }
}

What I am trying to work out.., but I have no idea how to, is; only run this action if there is already a user created/loaded. The reason for this is if there is no user loaded, it stops the page from loading correctly until a user is selected, we create users on the fly when doing admin orders, this snippet breaks things if you don't have a user to switch to.

So essentially the question is - Is there a way to only run the action only if there is a user id loaded?

Thanks Nik

1
+50

One way of doing this is check with the get_current_user() and check if return something, if yes, run the code. Like the example below:

add_action( 'woocommerce_admin_order_data_after_order_details', 'ccef_order_switch_to', 99 );
/**
 * Add a switch to user link on the order details.
 *
 * @param object $order The WC Order that we're working with.
 */
function ccef_order_switch_to( $order ) {
    $current_user = wp_get_current_user();
    $user_switching = $GLOBALS['user_switching'];

    if ( $user_switching && $current_user ) {
        $user        = get_user_by( 'id', $order->get_user_id() );
        $switch_link = ''; 

        // Only try to switch if a $user was set. 
        if( $user ) {
          $switch_link = $user_switching::maybe_switch_url( $user );
        } 

        if ( $switch_link ) {
            echo '<a href="' . esc_url( $switch_link ) . '">Switch To</a>';
        }
    }
}
  • I get a fatal error with this code (it may have been of fatal error before your changes). 'PHP Fatal error: Uncaught TypeError: Argument 1 passed to user_switching::maybe_switch_url() must be an instance of WP_User, boolean given, called in /srv/users/serverpilot/apps/domain/public/wp-content/themes/domain/functions.php on line 573 and defined in /srv/users/serverpilot/apps/domain/public/wp-content/plugins/user-switching/user-switching.php:607' can you see why it would throw an error like this when no user is already created for the order? – Nik Aug 1 '17 at 10:24
  • Yes, the functions expects a user to switch to, but thats strange since my code didn't override the $user variable that you were using, can you put a var_dump($user) below $user = get_user_by( 'id', $order->get_user_id() ); to see what is being printed please? – Cesar Henrique Damascena Aug 1 '17 at 11:29
  • This is what it prints prnt.sc/g30rz0 and this is the stack trace codeshare.io/5w4xYx – Nik Aug 1 '17 at 15:01
  • So, the problem is here $user = get_user_by( 'id', $order->get_user_id() ); he is not founding any user with the given id got by $order->get_user_id() . How do you set this property? – Cesar Henrique Damascena Aug 1 '17 at 15:04
  • I'm not sure how to work that out? – Nik Aug 1 '17 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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