WooCommerce Admin order – Only run action if user is loaded

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