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 ) {
echo '<pre>'; print_r($email->id); echo '</pre>';
}
代码放在您当前使用的子主题(或主题)的function.php文件中,或者也可以放在任何插件文件中。
您将获得以下之一:
对于WooCommerce默认通知:
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
low_stock
no_stock
对于WooCommerce订阅:
new_renewal_order
new_switch_order
customer_processing_renewal_order
customer_completed_renewal_order
customer_on_hold_renewal_order
customer_completed_switch_order
customer_renewal_invoice
cancelled_subscription
expired_subscription
suspended_subscription
对于WooCommerce预订:
new_booking
booking_reminder
booking_confirmed
booking_pending_confirmation
booking_notification
booking_cancelled
admin_booking_cancelled
对于WooCommerce会员资格:
WC_Memberships_User_Membership_Note_Email
WC_Memberships_User_Membership_Ending_Soon_Email
WC_Memberships_User_Membership_Ended_Email
WC_Memberships_User_Membership_Renewal_Reminder_Email
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 ) {
if( ! ( 'new_order' == $email->id ) ) return;
echo '<p>'.__('My custom text').'</p>';
}
将代码放入您的子主题的functions.php文件中(或者放在插件中)。
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