使用 Woocommerce 中的电子邮件 ID 定位特定电子邮件 [英] Targeting specific email with the email id in Woocommerce

查看:120
本文介绍了使用 Woocommerce 中的电子邮件 ID 定位特定电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Woocommerce 中设置了自定义状态和自定义电子邮件.我想使用当前电子邮件,WC_Email,而不是当前状态作为电子邮件模板中的变量.

我需要在电子邮件模板中有一些 if 语句.我没有使用订单状态来确保如果来自订单的电子邮件被手动重新发送,它不会通过单独的电子邮件发送当前订单状态的数据.

如何在 Woocommerce 中将 WC_Email 电子邮件 ID 作为变量回显?

解决方案

WooCommerce 中不存在 wc_order_email 类或函数,所以我更新了您的问题.

您正在查看的是 $email 变量参数 (WC_Email 当前类型对象).它主要在模板和钩子中随处定义.

要获取可用的当前电子邮件 ID 作为变量,您只需使用 $email_id = $email->id...

要获取自定义电子邮件的当前电子邮件 ID,您应该使用此代码(仅用于测试):

add_action( 'woocommerce_email_order_details', 'get_the_wc_email_id', 9, 4 );函数 get_the_wc_email_id( $order, $sent_to_admin, $plain_text, $email ) {//将输出当前通知的电子邮件 IDecho '

';print_r($email->id);echo '</pre>';}

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

您将获得以下内容:

  • new_order
  • customer_on_hold_order
  • customer_processing_order
  • customer_completed_order
  • customer_refunded_order
  • customer_partially_refunded_order
  • cancelled_order
  • failed_order
  • customer_reset_password
  • customer_invoice
  • customer_new_account
  • customer_note

一旦您为您的自定义电子邮件通知获得正确的电子邮件 ID slug,您就可以在任何以下钩子上使用它(而不是覆盖电子邮件模板):

woocommerce_email_header (2 个参数:$email_heading, $email)
woocommerce_email_order_details (4 个参数:$order$sent_to_admin$plain_text$电子邮件)
woocommerce_email_order_meta (4 个参数:$order$sent_to_admin$plain_text$电子邮件)
woocommerce_email_customer_details (4 个参数:$order$sent_to_admin$plain_text$电子邮件)
woocommerce_email_footer (1 个参数:$email)

这里是我定位新订单"的代码示例;仅电子邮件通知:

add_action( 'woocommerce_email_order_details', 'add_custom_text_to_new_order_email', 10, 4 );函数 add_custom_text_to_new_order_email( $order, $sent_to_admin, $plain_text, $email ) {//仅适用于新订单"电子邮件通知(将替换为您的)if(!('new_order' == $email->id)) 返回;//显示自定义文本(例如)echo '<p>'.__('我的自定义文本').'</p>';}

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

经过测试并有效.

I have custom statuses and custom emails set up in Woocommerce. I would like to use the current email, WC_Email, not the current status as a variable inside email templates.

I need to have some if statements in the email templates. I am not using the order status to ensure if an email from an order gets resent manually it doesn't send data for the current order status with an separate email.

How can I echo the WC_Email email ID as a variable in Woocommerce?

解决方案

The wc_order_email class or function doesn't exist in WooCommerce, so I have updated your question.

What you are looking at is $email variable argument (the WC_Email current type object). It's mostly defined everywhere in templates and hooks.

To get the usable current Email ID as a variable you will simply use $email_id = $email->id

To get the current Email ID of your custom emails, you should use this code (just for testing):

add_action( 'woocommerce_email_order_details', 'get_the_wc_email_id', 9, 4 );
function get_the_wc_email_id( $order, $sent_to_admin, $plain_text, $email ) {
    // Will output the email id for the current notification
    echo '<pre>'; print_r($email->id); echo '</pre>'; 
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

You will get the following:

  • new_order
  • customer_on_hold_order
  • customer_processing_order
  • customer_completed_order
  • customer_refunded_order
  • customer_partially_refunded_order
  • cancelled_order
  • failed_order
  • customer_reset_password
  • customer_invoice
  • customer_new_account
  • customer_note

Once you get the correct email ID slug for your custom email notification you can use it on any following hook (instead of overriding email templates):

woocommerce_email_header (2 arguments: $email_heading, $email)
woocommerce_email_order_details (4 arguments: $order, $sent_to_admin, $plain_text, $email)
woocommerce_email_order_meta (4 arguments: $order, $sent_to_admin, $plain_text, $email)
woocommerce_email_customer_details (4 arguments: $order, $sent_to_admin, $plain_text, $email)
woocommerce_email_footer (1 argument: $email)

HERE an example of code where I target "New order" email notifications only:

add_action( 'woocommerce_email_order_details', 'add_custom_text_to_new_order_email', 10, 4 );
function add_custom_text_to_new_order_email( $order, $sent_to_admin, $plain_text, $email ) {
    // Only for "New Order"  email notifications (to be replaced by yours)
    if( ! ( 'new_order' == $email->id ) ) return;

    // Display a custom text (for example)
    echo '<p>'.__('My custom text').'</p>';
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works.

这篇关于使用 Woocommerce 中的电子邮件 ID 定位特定电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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