如何从WooCommerce新订单电子邮件中获取电子邮件收件人

3

我想从WooCommerce电子邮件设置中获取“新订单”电子邮件的收件人字段值,如下所示:

enter image description here

如何获取收件人(s)字段? 任何帮助将不胜感激。

1个回答

4
您可以使用下面这行简单的代码来完成它:
WC()->mailer()->get_emails()['WC_Email_New_Order']->recipient;
// Or: WC()->mailer()->get_emails()['WC_Email_New_Order']->get_recipient();
// Or: WC()->mailer()->get_emails()['WC_Email_New_Order']->settings['recipient'];

或者详细说明(代码已经注释):

// Get an instance of the WC_emails Object 
$wc_emails = WC()->mailer();

// Get available emails notifications
$emails_array = $wc_emails->get_emails();

// Get the instance of the WC_Email_New_Order Object
$new_order_email = $emails_array['WC_Email_New_Order'];

// Get recipients from New Order email notification
$new_order_recipient = $new_order_email->recipient;
// Or $new_order_email->get_recipient(); 
// Or $new_order_email->settings['recipient'];
WC_Email_New_Order类是一种当收到/支付新订单时会发送给管理员的电子邮件(正如您在文档中看到的)。WC_Email方法get_recipient()在其源代码中使用$this->recipient,其中$this在这种情况下是WC_Email_New_Order对象(因为它扩展了WC_Email类)。您可以使用get_recipient()方法、属性recipientsettings['recipient']

谢谢您的回答, 但是如果我使用$new_order_email->get_recipient();,它将返回客户账单的电子邮件, 所以我更改为$new_order_email->recipient; 这样可以吗? - Tan Ky Bui
@TanKyBui 通常情况下,"新订单"通知被设置在WooCommerce设置中,以发送给管理员收件人。无论如何,我已经更新了我的代码以反映您的评论。您也可以使用:$new_order_email->settings['recipient']; - LoicTheAztec

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接