我有类似的问题:Switch to user link shortcode我尝试通过以下方式调整开发人员常见问题解答中的代码:
add_action( \'woocommerce_admin_order_data_after_billing_address\', \'display_admin_switch_user\', 10, 1 );
function display_admin_switch_user($order){
$data = $order->get_data(); // The Order data
$customer_id = $data[\'customer_id\'];
$customer = new WC_Customer( $customer_id );
if ( method_exists( \'user_switching\', \'maybe_switch_url\' ) ) {
$url = user_switching::maybe_switch_url( $customer );
if ( $url ) {
printf(
\'<a href="https://wordpress.org/plugins/user-switching/%1$s">Switch to %2$s</a>\',
esc_url( $url ),
esc_html( $customer->display_name )
);
}
}
}
但获取错误:
Fatal error: Uncaught TypeError: Argument 1 passed to user_switching::maybe_switch_url() must be an instance of WP_User, instance of WC_Customer given, called in /data/5/4/5467114b-e4f7-499d-b5d6-fb876c266fe2/perties.sk/sub/test/wp-content/plugins/code-snippets/php/snippet-ops.php(469) : eval()\'d code on line 9 and defined in /data/5/4/5467114b-e4f7-499d-b5d6-fb876c266fe2/perties.sk/sub/test/wp-content/plugins/user-switching/user-switching.php:757 Stack trace: #0 /data/5/4/5467114b-e4f7-499d-b5d6-fb876c266fe2/perties.sk/sub/test/wp-content/plugins/code-snippets/php/snippet-ops.php(469) : eval()\'d code(9): user_switching::maybe_switch_url(Object(WC_Customer)) #1 /data/5/4/5467114b-e4f7-499d-b5d6-fb876c266fe2/perties.sk/sub/test/wp-includes/class-wp-hook.php(307): display_admin_switch_user(Object(Automattic\\WooCommerce\\Admin\\Overrides\\Order)) #2 /data/5/4/5467114b-e4f7-499d-b5d6-fb876c266fe2/perties.sk/sub/test/wp-includes/class-wp-hook.php(331): WP_Hook->apply_filters(NULL, Array) #3 /data/5/4/5467114b-e4f7-499d-b5d6-fb876 in /data/5/4/5467114b-e4f7-499d-b5d6-fb876c266fe2/perties.sk/sub/test/wp-content/plugins/user-switching/user-switching.php on line 757
知道我做错了什么吗?谢谢
SO网友:Vlado
以下是测试代码:
add_action( \'woocommerce_admin_order_data_after_billing_address\', \'display_admin_switch_user\', 10, 1 );
function display_admin_switch_user($order){
$data = $order->get_data(); // The Order data
$user = $order->get_user();
$user_id = $order->get_user_id();
if ( method_exists( \'user_switching\', \'maybe_switch_url\' ) ) {
$url = user_switching::maybe_switch_url( $user );
if ( $url ) {
printf(
\'<a href="https://mysite.sk/plugins/user-switching/%1$s">Switch to %2$s</a>\',
esc_url( $url ),
esc_html( $user->display_name )
);
}
}
}