-
-
Save BeardedGinger/e58ab34416725fbc5ba02ea5262039de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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>'; | |
} | |
} | |
} |
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.
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>';
}
}
}
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
Is this working for you in 3.X?