Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@webthread

This comment has been minimized.

Copy link

@webthread webthread commented Jul 21, 2017

Is this working for you in 3.X?

@webthread

This comment has been minimized.

Copy link

@webthread webthread commented Jul 26, 2017

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

This comment has been minimized.

Copy link

@webthread 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

This comment has been minimized.

Copy link

@drrule 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
You can’t perform that action at this time.