如何在 WooCommerce 管理员订单列表中的买家姓名后添加第一条订单消息

How to add a first order message after the buyer name in WooCommerce admin order list

意图在所附图片中表示。本质上是显示在 WooCommerce 管理后端订单页面列表上。如果订单来自新客户(如果这是他的第一笔订单),那么管理员可以知道这是该人在商店的第一笔订单。

我发现我应该能够使用以下代码段检查它是否是第一笔订单。

function new_customer_warning( $document_type, $order )
{
    if( !empty($order) && $document_type == 'invoice' ) {
        if( $order->get_user_id() != 0 ) {
            $customer_orders = wc_get_customer_order_count( $order->get_user_id() );

            if( $customer_orders == 1 ) {
                echo '<tr><th><strong>New customer!</strong></th><td></td></tr>';
            }
        }
    }
}

但是我想不出管理订单页面的挂钩,以了解如何在那里显示预期的新订单警告。

作为替代方案(或者最好在两个地方都有)“新客户”消息可以直接在“订单详细信息”页面中回显。

此图显示了两个位置上此概念的示例:

您可以使用 manage_shop_order_posts_custom_columnwoocommerce_admin_order_data_after_order_details 操作挂钩

// Admin order list
function action_manage_shop_order_posts_custom_column( $column, $post_id ) {    
    // Compare
    if ( $column == 'order_number' ) {
        // Get order
        $order = wc_get_order( $post_id );
        
        // Getting the user ID
        $user_id = $order->get_user_id();

        // User ID exists
        if ( $user_id >= 1 ) {
            // Get the user order count
            $order_count = wc_get_customer_order_count( $user_id );
            
            // First order
            if ( $order_count == 1 ) {
                echo '&nbsp;<span>&ndash; ' .  __( 'New customer!', 'woocommerce' ) . '</span>';                
            }
        }
    }
}
add_action( 'manage_shop_order_posts_custom_column', 'action_manage_shop_order_posts_custom_column', 20, 2 );

// Order details page
function action_woocommerce_admin_order_data_after_order_details( $order ) {
    // Getting the user ID
    $user_id = $order->get_user_id();

    // User ID exists
    if ( $user_id >= 1 ) {
        // Get the user order count
        $order_count = wc_get_customer_order_count( $user_id );
        
        // First order
        if ( $order_count == 1 ) {
            echo '<h3>' .  __( 'New customer!', 'woocommerce' ) . '</h3>';              
        }
    }
}
add_action( 'woocommerce_admin_order_data_after_order_details', 'action_woocommerce_admin_order_data_after_order_details', 10, 1 );