如何在WooCommerce新订单邮件中隐藏买家邮箱(多商户场景)
嘿,这个需求在多商户场景下太常见了,毕竟要保护买家隐私嘛!我给你两种靠谱的实现方式,都是不会因为WooCommerce更新而失效的安全做法:
方法一:通过子主题自定义邮件模板
这种方法适合喜欢直观修改模板内容的开发者,步骤很清晰:
- 找到WooCommerce的原始邮件模板:在你的WordPress安装目录里,进入
wp-content/plugins/woocommerce/templates/emails,找到给商户/管理员的新订单模板new-order.php(别搞错成给买家的customer-new-order.php了)。 - 复制模板到子主题:在你的子主题文件夹里,新建
woocommerce/emails目录,把刚才找到的new-order.php复制进去。WooCommerce会自动优先加载子主题里的模板,完全不用担心插件更新覆盖你的修改。 - 编辑模板移除邮箱:打开子主题里的
new-order.php,找到这段显示买家邮箱的代码(类似下面这样):
直接删掉这段,或者用PHP注释把它藏起来(比如<p><?php printf( esc_html__( 'Customer email: %s', 'woocommerce' ), '<a href="mailto:' . esc_attr( $order->get_billing_email() ) . '">' . esc_html( $order->get_billing_email() ) . '</a>' ); ?></p><?php /* 这里是被注释掉的邮箱代码 */ ?>)。 - 保存文件后,下测试单看看商户收到的邮件,买家邮箱就消失啦。
方法二:用钩子函数动态移除邮箱内容
如果你不想碰模板文件,用代码钩子的方式更灵活,也方便后续调整:
- 打开子主题的
functions.php文件,或者用专门的代码片段插件(比如Code Snippets)添加下面这段代码:add_action( 'woocommerce_email_customer_details', 'remove_billing_email_from_merchant_new_order', 20, 4 ); function remove_billing_email_from_merchant_new_order( $order, $sent_to_admin, $plain_text, $email ) { // 只针对给商户/管理员的新订单邮件生效 if ( $email->id == 'new_order' ) { // 移除默认的客户详情输出 remove_action( 'woocommerce_email_customer_details', 'woocommerce_email_customer_details', 10 ); // 替换成我们自定义的客户详情,不含邮箱 add_action( 'woocommerce_email_customer_details', 'custom_woocommerce_email_customer_details', 10, 4 ); } } function custom_woocommerce_email_customer_details( $order, $sent_to_admin, $plain_text, $email ) { $text_align = is_rtl() ? 'right' : 'left'; ?> <table id="customer-details" class="td" cellspacing="0" cellpadding="0" style="width: 100%; margin-bottom: 2em;" border="0"> <tr> <th class="td" scope="row" style="text-align:<?php echo esc_attr( $text_align ); ?>; width: 50%;"><?php esc_html_e( 'Customer details', 'woocommerce' ); ?></th> <td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; width: 50%;"><?php echo wp_kses_post( $order->get_formatted_billing_full_name() ); ?></td> </tr> <?php if ( $order->get_billing_phone() ) : ?> <tr> <th class="td" scope="row" style="text-align:<?php echo esc_attr( $text_align ); ?>; width: 50%;"><?php esc_html_e( 'Phone:', 'woocommerce' ); ?></th> <td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; width: 50%;"><?php echo esc_html( $order->get_billing_phone() ); ?></td> </tr> <?php endif; ?> </table> <?php } - 这段代码的逻辑是:当系统发送给商户的新订单邮件时,先取消默认的客户详情显示,然后替换成我们自己写的版本——只保留买家姓名和电话(如果有填写的话),完全去掉了邮箱信息。
- 保存代码后,测试一下新订单通知,就能看到效果了。
重要提醒
- 绝对不要直接修改WooCommerce插件目录里的文件!插件一更新,你的修改就全没了,子主题和钩子才是正确姿势。
- 如果用代码片段插件,记得启用之后再测试,避免语法错误导致网站出问题(可以先在本地环境测试哦)。
内容的提问来源于stack exchange,提问作者Ziga108
相关产品推荐
相关产品推荐

