繁体   English   中英

在 WooCommerce 中为新客户订单添加订单备注

[英]Add order note to new customer orders in WooCommerce

我正在尝试添加一项功能,其中将为所有首次客户添加管理订单注释。

我正在尝试的代码是:

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();
        }
}

但是,问题是在每个订单上添加新的客户注释。 有什么建议吗?

您的代码看起来很好,除了这里的语法错误。 我修改了你的代码。 试试下面的代码。

改变了

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

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

改变了

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

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

检查客户是第一次尝试以下条件。

if ( count( $customer_orders ) < 1 ) {

检查客户是退货。 尝试以下条件。

if ( count( $customer_orders ) > 0 ) {

完整的代码。 对于回头客

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

首次客户的完整代码

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

其实go是没有必要通过订单的,因为在WooCommerce中已经有一个function,即wc_get_customer_order_count() - get total orders by customer。

只有这里的问题是,就像您当前的代码一样,这仅适用于已登录的用户

要使这也适用于“客人” ,您可以使用以下内容:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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