douzheng9221 2018-04-04 13:07
浏览 164
已采纳

在Woocommerce管理订单中显示已完成订单的用户名

In woocommerce how to show in admin order list which user has mark as "completed" the order like in this screenshot:

IMAGE

  • 写回答

1条回答 默认 最新

  • duanjue7745 2018-04-04 16:49
    关注

    This can be done with the following… But it will be generated for the orders which status has been changed to completed after starting when you will add this code:

    // Save backend user and time for completion order
    add_action('woocommerce_order_status_completed', 'user_has_completed_order');
    function user_has_completed_order( $order_id ) {
        if ( ! is_admin() ) return;
    
        $user_id = get_current_user_id();
    
        if ( ! empty( $user_id ) && $user_id != 0 ) {
            $now = date('Y-m-d H:i:s');
            update_post_meta( $order_id, '_completed_by_user', $user_id );
            update_post_meta( $order_id, '_completed_datetime', $now );
        }
    }
    
    // Adding 1 new columns to backend order list
    add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column',11);
    function custom_shop_order_column($columns)
    {
        $reordered_columns = array();
    
        // Woocommerce version 3.3+ compatibility
        $location_after = version_compare( WC_VERSION, '3.3', '<' ) ? 'order_notes' : 'order_status';
    
        // Inserting the new column in a specific location
        foreach( $columns as $key => $column){
            $reordered_columns[$key] = $column;
            if( $key == $location_after ){
                $reordered_columns['completed_by'] = __( 'Completed by','woocommerce');
            }
        }
        return $reordered_columns;
    }
    
    // Adding the data for the additional column (example)
    add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 10, 2 );
    function custom_orders_list_column_content( $column, $post_id )
    {
        if( 'completed_by' == $column )
        {
            // Get custom post meta data
            $user_id = get_post_meta( $post_id, '_completed_by_user', true );
            $date_time = get_post_meta( $post_id, '_completed_datetime', true );
            $date = date('Y-m-d', strtotime($date_time));
            if( ! empty($user_id) ){
                $user_data = get_userdata( $user_id );
                $user_url = get_edit_user_link( $user_id );
                echo '<a href="'.$user_url.'" title="'.$date_time.'">'.$user_data->user_login.'</a>';
            }
        }
    }
    

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

    It works as well in all woocommerce versions included last version 3.3 where order list has been slightly improved:

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含