
本文将指导您如何通过自定义代码,在WooCommerce的特定订单邮件通知中移除产品购买备注。默认情况下,购买备注会出现在订单确认邮件和订单完成邮件中。但有时,您可能希望仅在订单确认邮件中显示这些备注,而在订单完成邮件中将其隐藏。以下步骤将帮助您实现这一目标。
直接使用woocommerce_email_order_items_args过滤器移除购买备注,会影响所有订单邮件通知。我们需要一种方法来区分不同的邮件类型,并仅对特定邮件类型应用修改。
WooCommerce并没有直接在woocommerce_email_order_items_args钩子中暴露邮件ID。因此,我们需要借助另一个钩子woocommerce_email_before_order_table,将邮件ID设置为全局变量,以便在woocommerce_email_order_items_args钩子中使用。
以下代码实现了这个功能:
// 设置邮件ID为全局变量
function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {
$GLOBALS['email_id_str'] = $email->id;
}
add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 10, 4 );这段代码会在每个订单邮件的表格之前执行,并将当前邮件的ID存储在名为$email_id_str的全局变量中。
现在,我们可以使用woocommerce_email_order_items_args过滤器,并检查全局变量$email_id_str的值,以确定是否需要移除购买备注。
function filter_woocommerce_email_order_items_args( $args ) {
// 获取邮件ID全局变量
$refNameGlobalsVar = $GLOBALS;
$email_id = isset( $refNameGlobalsVar['email_id_str'] ) ? $refNameGlobalsVar['email_id_str'] : '';
// 目标邮件类型,多个类型用逗号分隔
if ( in_array( $email_id, array( 'customer_completed_order', 'customer_invoice' ) ) ) {
// 移除购买备注
$args['show_purchase_note'] = false;
}
return $args;
}
add_filter( 'woocommerce_email_order_items_args', 'filter_woocommerce_email_order_items_args', 10, 1 );这段代码首先获取全局变量$email_id_str的值。然后,它检查该值是否在customer_completed_order(订单完成邮件)和customer_invoice(客户发票邮件)的数组中。如果匹配,则将$args['show_purchase_note']设置为false,从而移除购买备注。
将以上代码添加到您的 WordPress 主题的 functions.php 文件中,或者使用代码片段插件(如Code Snippets)。
注意事项:
通过使用全局变量和woocommerce_email_order_items_args过滤器,您可以精确控制哪些WooCommerce订单邮件通知中显示产品购买备注。这种方法提供了更大的灵活性,允许您根据您的特定需求定制WooCommerce的邮件通知。记住,在应用任何代码更改之前,始终进行备份和测试。
以上就是WooCommerce教程:有选择地从订单邮件通知中移除产品购买备注的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号