如何在WooCommerce本地取货邮件通知中隐藏收货地址?
WooCommerce本地取货时隐藏邮件中的收货地址
可以通过添加代码片段实现该需求,无需修改邮件模板文件(避免主题/插件更新导致配置失效),具体方案如下:
基础代码实现
将以下代码添加到当前主题的functions.php文件末尾,或使用代码片段插件(如Code Snippets)导入:
add_action( 'woocommerce_email_customer_details', 'hide_shipping_address_for_local_pickup_in_emails', 5, 4 ); function hide_shipping_address_for_local_pickup_in_emails( $order, $sent_to_admin, $plain_text, $email ) { // 遍历订单配送方式,检查是否为本地取货 $shipping_methods = $order->get_shipping_methods(); foreach ( $shipping_methods as $shipping_method ) { // 匹配WooCommerce默认本地取货的方法ID(自定义取货方式需替换为对应ID) if ( strpos( $shipping_method->get_method_id(), 'local_pickup' ) !== false ) { // 移除邮件中收货地址的输出动作 remove_action( 'woocommerce_email_customer_details', 'woocommerce_display_order_shipping_address', 10 ); break; } } }
代码说明
- 借助
woocommerce_email_customer_details钩子在邮件客户详情渲染前执行判断,优先级设为5(早于默认地址输出的优先级10); - 通过
get_shipping_methods()获取订单配送方式,检查是否包含local_pickup(WooCommerce默认本地取货的标识); - 匹配到本地取货时,移除默认的收货地址输出动作,从而隐藏邮件中的收货地址。
自定义适配
如果使用第三方插件或自定义本地取货方式,需将代码中的local_pickup替换为对应配送方式的方法ID:
- 进入WooCommerce后台 → 配送 → 对应取货方式的设置页面查看ID;
- 或在订单编辑页面,查看配送方式的标识文本。
可选:仅隐藏客户邮件中的地址
若需要管理员邮件保留收货地址,仅在客户收到的邮件中隐藏,可使用以下代码:
add_action( 'woocommerce_email_customer_details', 'hide_shipping_address_for_local_pickup_in_customer_emails', 5, 4 ); function hide_shipping_address_for_local_pickup_in_customer_emails( $order, $sent_to_admin, $plain_text, $email ) { // 跳过管理员收件的邮件 if ( $sent_to_admin ) return; $shipping_methods = $order->get_shipping_methods(); foreach ( $shipping_methods as $shipping_method ) { if ( strpos( $shipping_method->get_method_id(), 'local_pickup' ) !== false ) { remove_action( 'woocommerce_email_customer_details', 'woocommerce_display_order_shipping_address', 10 ); break; } } }
使用注意
- 添加代码前建议备份主题文件或使用代码片段插件,避免操作失误导致网站异常;
- 测试本地取货订单的自动邮件,确认收货地址已被隐藏。
内容的提问来源于stack exchange,提问作者Himmler Fleur
相关产品推荐
相关产品推荐

