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

Posted

技术标签:

【中文标题】在 WooCommerce 中为新客户订单添加订单备注【英文标题】:Add order note to new customer orders in WooCommerce 【发布时间】:2022-01-20 02:38:49 【问题描述】:

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

我正在尝试的代码是:

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

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

【问题讨论】:

【参考方案1】:

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

已更改

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

【讨论】:

【参考方案2】:

其实没有必要去查看订单,因为在 WooCommerce 中已经有这样的功能,即wc_get_customer_order_count() - 按客户获取订单总数。

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

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

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://***.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->prefixposts AS p
        INNER JOIN $wpdb->prefixpostmeta 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 ); 


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

【讨论】:

以上是关于在 WooCommerce 中为新客户订单添加订单备注的主要内容,如果未能解决你的问题,请参考以下文章

如何将 WordPress 用户角色添加到 Woocommerce 新管理员订单电子邮件主题

如何在 WooCommerce 编辑订单中计算自定义订单总额?

在 Woocommerce 中为订单添加额外的元数据

在Woocommerce中为订单添加额外的元数据

Woocommerce 我的帐户页面为新客户运行了一个额外的循环

在客户订单历史记录中显示最后一个WooCommerce管理订单备注