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 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题