获取新订单中的客户订单电子邮件通知

问题描述:

我想在WooCommerce的“新订单”电子邮件通知中指出,如果它是重复客户。获取新订单中的客户订单电子邮件通知

看起来很简单,但我试过5种不同的方法,都没有工作。我已经试过把这个放入2个不同的挂钩:

  • woocommerce_email_after_order_table
  • woocommerce_email_subject_new_order

好像wc_get_customer_order_count($user->ID)应该工作,但它看来,$user对象不传递到那些挂钩的功能,对不对?

我也想知道,如果这是可能的,当它是一个客人,而不是一个注册用户,也许通过比较电子邮件地址?

感谢

WooCommerce电子邮件通知相关订单。

woocommerce_email_after_order_table钩子中,您将Order对象作为钩住自定义函数中的参数,并且还有$email对象。

随着该$order对象,你可以得到user ID这样:

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

$email对象,你可以针对新订单的电子邮件通知。

所以工作代码将是:

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钩例如...

代码放在您的活动子主题的function.php文件(或主题)或任何插件文件。

此代码已经过测试并可正常工作。

+0

这太好了,谢谢!希望我会早点提问。 :-P – protohominid