questions

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

Rate this post

The intention is represented on the attached Image. Essentially to display on WooCommerce admin backend order page list. If the order comes from a new Customer (if it is his first order) so that admins can know it’s a persons first order on the store.

I have figured out that I should be able to check if it is a first order with the following snippet.

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>';
            }
        }
    }
}

However I can’t figure out a hook for the admin Orders page, for how to display the intended New Order warning there.

As an alternative (or ideally having it in both places) The “New Customer” message could be echoed directly inside of the Order Details page.

This image shows an example of this concept on both locations:

ss2

 

✔️Solution:

You could use the manage_shop_order_posts_custom_column and woocommerce_admin_order_data_after_order_details action hook

// 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 );

Leave a Reply

Your email address will not be published.

Back to top button