在新订单电子邮件通知中获取客户订单数 [英] Get customer order count in new order email notification

查看:68
本文介绍了在新订单电子邮件通知中获取客户订单数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果是回头客,我想在 WooCommerce 的新订单"电子邮件通知中注明.

I'd like to indicate in WooCommerce "new order" email notification, if it's a repeat customer.

看起来很简单,但我尝试了大约 5 种不同的方法,但都没有奏效.我试过把它放到 2 个不同的钩子里:

It seems simple, but I've tried about 5 different methods and none worked. I've tried putting this into 2 different hooks:

  • woocommerce_email_after_order_table
  • woocommerce_email_subject_new_order.

似乎 wc_get_customer_order_count($user->ID) 应该可以工作,但看起来 $user> 对象没有传递到那些钩子的函数中,对吧?

Seems like wc_get_customer_order_count($user->ID) should work but it appears that the $user object is not passed into those hook's functions, right?

我还想知道,如果是访客而不是注册用户,是否可以通过比较电子邮件地址来实现这一点?

I'm also wondering if this is possible when it's a guest and not a registered user, maybe by comparing the email address?

谢谢

推荐答案

WooCommerce 电子邮件通知与订单相关.

woocommerce_email_after_order_table 钩子中,您将 Order 对象作为钩子自定义函数和 $email 对象中的参数.

In woocommerce_email_after_order_table hook, you have the Order object as an argument in your hooked custom function and also the $email object.

使用 $order 对象,您可以通过以下方式获取 user ID:

With that $order object, you can get the user ID this way:

$user_id = $user_id = $order->get_user_id();

您可以从 $email 对象中定位新订单电子邮件通知.

From the $email object you can target the new order email notification.

所以工作代码将是:

add_action( 'woocommerce_email_after_order_table', 'customer_order_count', 10, 4);
function customer_order_count( $order, $sent_to_admin, $plain_text, $email ){

    if ( $order->get_user_id() > 0 ){

        // Targetting new orders (that will be sent to customer and to shop manager)
        if ( 'new_order' == $email->id ){

            // Getting the user ID
            $user_id = $order->get_user_id();

            // Get the user order count
            $order_count = wc_get_customer_order_count( $user_id );

            // Display the user order count
            echo '<p>Customer order count: '.$order_count.'</p>';

        }
    }
}

例如,您也可以使用 woocommerce_email_before_order_table 钩子来代替...

You can also use instead the woocommerce_email_before_order_table hook for example…

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.

此代码已经过测试且有效.

This code is tested and works.

这篇关于在新订单电子邮件通知中获取客户订单数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆