简体   繁体   中英

Add order note to new customer orders in WooCommerce

I'm trying to add in a feature where an admin order note will be added for all first time customers.

The code I am trying is:

add_action('woocommerce_thankyou', 'is_returning_customer', 10, 1);

function is_returning_customer($order_id) 
{
    if (!$order_id) {
        return;
    }
    if(is_user_logged_in()) {
        $order_status = array('wc-on-hold,','wc-processing', 'wc-completed');
        $customer_id = get_current_user_id(); 
            $customer_orders=get_posts( array(
                'meta_key' => '_customer_user',
                'meta_value' => $customer_id,
                'post_type' => 'shop_order', 
                'post_status' => $order_status,
                'numberposts' => -1
            )
        );
     
    }
       if (count($customer_orders) => 1) {
        $order = wc_get_order( $order_id );
            $note = '*** New Customer ***';
            $order->add_order_note($note);
            $order->save();
        }
}

However, the problem it's adding the new customer note on every order. Any advice?

Your code looks fine except for syntax errors here. I revised your code. try the below code.

Changed

if (count($customer_orders) => 1) {

To

if (count($customer_orders) >= 1) {

Changed

$order_status = array( 'wc-on-hold,','wc-processing', 'wc-completed' );

To

$order_status = array( 'wc-on-hold', 'wc-processing', 'wc-completed' );

for check customer is first time trying below condition.

if ( count( $customer_orders ) < 1 ) {

for check customer is a return. try the below condition.

if ( count( $customer_orders ) > 0 ) {

Complete code. for returning customer

function is_returning_customer( $order_id ) {

    if ( !$order_id ) {
        return;
    }

    if( is_user_logged_in() ) {

        $order_status = array( 'wc-on-hold,','wc-processing', 'wc-completed' );
        $customer_id  = get_current_user_id(); 

            $customer_orders = get_posts( array(
                'meta_key'    => '_customer_user',
                'meta_value'  => $customer_id,
                'post_type'   => 'shop_order', 
                'post_status' => $order_status,
                'numberposts' => -1
            )
        );
     
    }

    if ( count( $customer_orders ) > 0 ) {
        $order = wc_get_order( $order_id );
        $note  = '*** New Customer ***';
        $order->add_order_note($note);
        $order->save();
    }
}
add_action( 'woocommerce_thankyou', 'is_returning_customer', 10, 1 );

Complete code for first time customer

function is_first_time_customer( $order_id ) {

    if ( !$order_id ) {
        return;
    }

    if( is_user_logged_in() ) {

        $order_status = array( 'wc-on-hold,','wc-processing', 'wc-completed' );
        $customer_id  = get_current_user_id(); 

            $customer_orders = get_posts( array(
                'meta_key'    => '_customer_user',
                'meta_value'  => $customer_id,
                'post_type'   => 'shop_order', 
                'post_status' => $order_status,
                'numberposts' => -1
            )
        );
     
    }

    if ( count( $customer_orders ) < 1 ) {
        $order = wc_get_order( $order_id );
        $note  = '*** New Customer ***';
        $order->add_order_note($note);
        $order->save();
    }
}
add_action( 'woocommerce_thankyou', 'is_first_time_customer', 10, 1 );

Actually it is not necessary to go through the orders, as there is already a function for this in WooCommerce, namely wc_get_customer_order_count() - Get total orders by customer.

Only the issue here is, just like with your current code, that this would only work for logged in users .

To make this also work for 'guests' you can use the following:

function action_woocommerce_thankyou( $order_id ) {
    // Get $order object
    $order = wc_get_order( $order_id );
    
    // Is a WC_Order
    if ( is_a( $order, 'WC_Order' ) ) {
        // Get user id
        $user_id = $order->get_user_id();
        
        // Set variable
        $count = 0;
        
        // Not a guest
        if ( $user_id > 0 ) {
            // Get the total orders by a customer.
            $count = wc_get_customer_order_count( $user_id );
        } else {                
            // Guest 'user', don't have a user id so we will determine the previous orders based on the billing email
            
            // Get billing email
            $billing_email = $order->get_billing_email();
            
            if ( ! empty ( $billing_email ) ) {
                // Call function
                $count = has_guest_bought_by_email( $billing_email );
            }
        }
        
        // Output
        if ( $count == 1 ) {
            $note  = '** New Customer **';
            $order->add_order_note( $note );
        }
    }
}
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );

// Based on https://stackoverflow.com/a/46216073/11987538
function has_guest_bought_by_email( $email ) {
    global $wpdb;
    
    // Get all order statuses.
    $order_statuses = array_map( 'esc_sql', array_keys( wc_get_order_statuses() ) );

    $results = $wpdb->get_col( "
        SELECT p.ID FROM {$wpdb->prefix}posts AS p
        INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
        WHERE p.post_status IN ( '" . implode( "','", $order_statuses ) . "' )
        AND p.post_type LIKE 'shop_order'
        AND pm.meta_key = '_billing_email'
        AND pm.meta_value = '$email'
    " );

    // Return result
    return count( $results ); 
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM