XMQ*_*snb 2 php wordpress orders email-notifications woocommerce
我在Woocommerce中设置了自定义状态和自定义电子邮件。我想将当前电子邮件(WC_Email而不是当前状态)用作电子邮件模板中的变量。
我需要在电子邮件模板中包含一些if语句。我不使用订单状态来确保是否手动重新发送来自订单的电子邮件,它不会通过单独的电子邮件发送当前订单状态的数据。
如何WC_Email在Woocommerce中将电子邮件ID作为变量进行回显?
在wc_order_email类或函数不存在WooCommerce,所以我已经更新了你的问题。
您正在查看的是$email变量参数(WC_Email当前类型对象)。它通常在模板和挂钩中随处定义。
要获得可用的当前电子邮件ID作为变量,您只需使用$email_id = $email->id…
要获取自定义电子邮件的当前电子邮件ID,应使用以下代码(仅用于测试):
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>';
}
Run Code Online (Sandbox Code Playgroud)
代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。
一旦为您的自定义电子邮件通知获取了正确的电子邮件ID段,就可以在以下任何挂钩上使用它(而不是覆盖电子邮件模板):
• woocommerce_email_header (2个参数:$email_heading,$email)
• woocommerce_email_order_details (4个参数:$order,$sent_to_admin,$plain_text,$email)
• woocommerce_email_order_meta (4个参数:$order,$sent_to_admin,$plain_text,$email)
• woocommerce_email_customer_details (4个参数:$order,$sent_to_admin,$plain_text,$email)
• woocommerce_email_footer (1个参数:$email)
这里有一个示例代码,我仅针对“新订单”电子邮件通知:
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>';
}
Run Code Online (Sandbox Code Playgroud)
代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。
经过测试和工作。