I want to update WooCommerce status for a order wi

2019-06-14 18:24发布

I am able to update status using this code In this image highlighted text is username of currently logged in user, when I changed status from dashboard it shows me name, but when I change status using code it won't show any name.

I want username should be display like in this screenshot:

Red boxed note

2条回答
仙女界的扛把子
2楼-- · 2019-06-14 18:45
add_filter('woocommerce_new_order_note_data', 'modify_added_by');

function modify_added_by($args) {

    $user = get_user_by('id', get_current_user_id());
    $comment_author = $user->display_name;
    $comment_author_email = $user->user_email;
    $args['comment_author'] = $comment_author;
    $args['comment_author_email'] = $comment_author_email;
}

Try this code

查看更多
疯言疯语
3楼-- · 2019-06-14 18:53

You can use the following hooked function to get the shop manager user name in the order note:

add_filter( 'woocommerce_new_order_note_data', 'filter_woocommerce_new_order_note_data', 10, 2 );
function filter_woocommerce_new_order_note_data( $args, $args2 ) {
    if( ! $args2['is_customer_note'] && is_user_logged_in() && current_user_can( 'edit_shop_order', $args2['order_id'] ) ){
        $user = get_user_by( 'id', get_current_user_id() );
        $args['comment_author'] = $user->display_name;
        $args['comment_author_email'] = $user->user_email;
    }

    return $args;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Related thread: Add the Shop Manager username to Woocommerce Admin Order notes

查看更多
登录 后发表回答