在WooCommerce中,通过电子邮件ID来针对特定的电子邮件通知进行定位。

7
我在WooCommerce中设置了自定义状态和自定义电子邮件。我想在电子邮件模板中使用当前的电子邮件WC_Email,而不是当前的状态作为变量。
我需要在电子邮件模板中使用一些if语句。我不使用订单状态来确保如果手动重新发送订单的电子邮件,则不会将当前订单状态的数据与单独的电子邮件一起发送。
我如何在WooCommerce中将WC_Email电子邮件ID作为变量输出?
1个回答

19

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>'; 
}

代码放在您当前使用的子主题(或主题)的function.php文件中,或者也可以放在任何插件文件中。
您将获得以下之一:
对于WooCommerce默认通知:
  1. new_order
  2. customer_on_hold_order
  3. customer_processing_order
  4. customer_completed_order
  5. customer_refunded_order
  6. customer_partially_refunded_order
  7. cancelled_order
  8. failed_order
  9. customer_reset_password
  10. customer_invoice
  11. customer_new_account
  12. customer_note
  13. low_stock
  14. no_stock
对于WooCommerce订阅:
  1. new_renewal_order
  2. new_switch_order
  3. customer_processing_renewal_order
  4. customer_completed_renewal_order
  5. customer_on_hold_renewal_order
  6. customer_completed_switch_order
  7. customer_renewal_invoice
  8. cancelled_subscription
  9. expired_subscription
  10. suspended_subscription
对于WooCommerce预订:
  1. new_booking
  2. booking_reminder
  3. booking_confirmed
  4. booking_pending_confirmation
  5. booking_notification
  6. booking_cancelled
  7. admin_booking_cancelled
对于WooCommerce会员资格:
  1. WC_Memberships_User_Membership_Note_Email
  2. WC_Memberships_User_Membership_Ending_Soon_Email
  3. WC_Memberships_User_Membership_Ended_Email
  4. WC_Memberships_User_Membership_Renewal_Reminder_Email
  5. WC_Memberships_User_Membership_Activated_Email
一旦您获得了自定义电子邮件通知的正确电子邮件ID slug,您可以在任何后续的钩子中使用它(而不是覆盖电子邮件模板):
- 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>';
}

将代码放入您的子主题的functions.php文件中(或者放在插件中)。

谢谢!我成功地让这个变体工作了。 - logrexton
将代码添加到您答案的第一部分中,以测试ID,对我来说不起作用:Fatal error: Uncaught ArgumentCountError: Too few arguments to function add_action(), 1 passed in /home/*****/public_html/framework/wp-content/themes/sgone/woocommerce/emails/email-header.php on line 70 and at least 2 expected 实际上,无论我尝试使用哪个钩子,这都是原因,并且$email是未定义的变量。 - Skovsgaard
看起来我已经修复了错误,你的代码实际上是可以工作的。只是它不适用于WooCommerce订阅的电子邮件模板。那么,有没有办法使其也能工作呢? - Skovsgaard
1
@Skovsgaard 再次测试...第一个测试电子邮件 ID 的函数完美无误,没有任何错误。现在对于 WooCommerce 订阅,情况有所不同,因为它不使用相同的钩子函数参数... - LoicTheAztec
是的,我的错。现在我已经让WooCommerce订阅邮件正常工作了。我在代码中犯了一个小错误。抱歉。 :) - Skovsgaard

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