Skip to content

Instantly share code, notes, and snippets.

@BeardedGinger
Created April 11, 2017 19:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BeardedGinger/e58ab34416725fbc5ba02ea5262039de to your computer and use it in GitHub Desktop.
Save BeardedGinger/e58ab34416725fbc5ba02ea5262039de to your computer and use it in GitHub Desktop.
@webthread
Copy link

Facepalm, I was testing it using an order I had created, so obviously no need to switch to myself.
Question, how hard would it be to not allow this to run on new admin generated orders, the reason being is it doesn't load anything after the switchTo link if it there is no account that it can find.

@webthread
Copy link

webthread commented Aug 1, 2017

This seems to do the trick, it stops a fatal error if no user is already associated with the order.

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

@drrule
Copy link

drrule commented Dec 24, 2018

Can you help me out with a simple question?

Do I add this to an exhisting page or do I create a new .php and save it in the plugin folder? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment