本文介绍如何使用 `woocommerce_email_after_order_table` 钩子,在 woocommerce 的“订单待处理”“订单处理中”和“订单已完成”三类客户邮件底部(订单表格后)精准插入针对非美国收货地址的定制化提示语。
在 WooCommerce 中,为不同地区客户定制邮件内容是提升本地化体验的重要手段。若需仅对发货国家非美国(即 shipping_country ≠ 'US') 的客户,在特定订单状态邮件(customer_on_hold_order、customer_processing_order、customer_completed_order)中追加说明性内容(例如物流时效提示、关税说明等),应避免依赖未定义变量(如原代码中的 $woocommerce),而直接通过传入的 $order 对象获取关键信息。
以下是经过验证、可直接集成到主题 functions.php 或专用插件中的标准实现:
/**
* 在指定客户邮件末尾添加非美国订单专属提示
*
* @param WC_Order $order 当前订单对象
* @param bool $sent_to_admin 是否发送给管理员(此处忽略,仅面向客户)
* @param bool $plain_text 是否为纯文本邮件格式
* @param WC_Email $email 当前邮件实例
*/
function action_woocommerce_email_after_order_table( $order, $sent_to_admin, $plain_text, $email ) {
// 限定作用范围:仅对三类客户通知生效
$target_emails = array(
'customer_on_hold_order',
'customer_processing_order',
'customer_completed_order'
);
if ( ! in_array( $email->id, $target_emails ) ) {
return;
}
// 判断是否为非美国发货地址(注意:使用 get_shipping_country(),非 billing_country)
if ( $order->get_shipping_country() !== 'U
S' ) {
// 支持 HTML 邮件格式;若需兼容纯文本邮件,请额外判断 $plain_text
if ( $plain_text ) {
echo "Note: This order ships outside the USA. Customs duties and delivery times may vary.\n";
} else {
echo 'Note: This order ships outside the USA. Customs duties and delivery times may vary.
';
}
}
}
add_action( 'woocommerce_email_after_order_table', 'action_woocommerce_email_after_order_table', 10, 4 );✅ 关键要点说明:
⚠️ 注意事项:
$country = $order->get_shipping_country() ?: $order->get_billing_country();
通过此方案,您即可实现精准、稳定、可维护的地域化邮件内容增强,无需修改 WooCommerce 核心模板文件。