The User Switching plugin is a great way to quickly see how Users at different access levels will interact with your site. It’s a necessary plugin for most development environments. (it also fits into our guidelines for extending WordPress for enterprise use)
However, it can also be a great plugin for site admins working on the day-to-day management of their site. For this snippet, we’re connecting the User Switching plugin to WooCommerce orders and allowing site administrators to quickly switch to the User associated with the current order they’re viewing.
The code to add User Switching to a WooCommerce Order
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>'; | |
} | |
} | |
} |