如何仅移除WooCommerce已完成订单邮件中的支付方式?
解决WooCommerce仅移除已完成订单邮件中支付方式的问题
问题说明
需要仅移除WooCommerce已完成订单通知邮件内的支付方式信息,不影响「订单处理中」等其他状态的邮件。此前尝试添加以下代码但未生效:
add_filter( 'woocommerce_get_order_item_totals', 'adjust_woocommerce_get_order_item_totals' ); function adjust_woocommerce_get_order_item_totals( $totals ) { unset($totals['cart_subtotal'] ); unset( $totals['payment_method'] ); return $totals; } ?>
解决方案
可以通过修改已完成订单的邮件模板实现需求,核心是让过滤器仅作用于当前邮件的订单详情模块,执行后立即移除过滤器避免影响其他场景。
修改后的完整模板代码
替换yourtheme/woocommerce/emails/customer-completed-order.php的内容为:
<?php /** * Customer completed order email * * This template can be overridden by copying it to yourtheme/woocommerce/emails/customer-completed-order.php. * * HOWEVER, on occasion WooCommerce will need to update template files and you * (the theme developer) will need to copy the new files to your theme to * maintain compatibility. We try to do this as little as possible, but it does * happen. When this occurs the version of the template file will be bumped and * the readme will list any important changes. * * @see https://docs.woocommerce.com/document/template-structure/ * @package WooCommerce\Templates\Emails * @version 3.7.0 */ if ( ! defined( 'ABSPATH' ) ) { exit; } /* * @hooked WC_Emails::email_header() Output the email header */ do_action( 'woocommerce_email_header', $email_heading, $email ); ?> <?php /* translators: %s: Customer first name */ ?> <p><?php printf( esc_html__( 'Hi %s,', 'woocommerce' ), esc_html( $order->get_billing_first_name() ) ); ?></p> <p><?php esc_html_e( 'We have finished processing your order.', 'woocommerce' ); ?></p> <?php /* * @hooked WC_Emails::order_details() Shows the order details table. * @hooked WC_Structured_Data::generate_order_data() Generates structured data. * @hooked WC_Structured_Data::output_structured_data() Outputs structured data. * @since 2.5.0 */ // 添加过滤器,仅移除支付方式 add_filter( 'woocommerce_get_order_item_totals', 'remove_completed_order_payment_method', 10, 1 ); function remove_completed_order_payment_method( $totals ) { // 仅移除支付方式项,保留其他总计内容 if ( isset( $totals['payment_method'] ) ) { unset( $totals['payment_method'] ); } return $totals; } do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); // 立即移除过滤器,避免影响后续其他邮件或功能 remove_filter( 'woocommerce_get_order_item_totals', 'remove_completed_order_payment_method', 10 ); /* * @hooked WC_Emails::order_meta() Shows order meta data. */ do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email ); /* * @hooked WC_Emails::customer_details() Shows customer details * @hooked WC_Emails::email_address() Shows email address */ do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text, $email ); /** * Show user-defined additional content - this is set in each email's settings. */ if ( $additional_content ) { echo wp_kses_post( wpautop( wptexturize( $additional_content ) ) ); } /* * @hooked WC_Emails::email_footer() Output the email footer */ do_action( 'woocommerce_email_footer', $email );
关键修改点
- 精准控制过滤器作用范围:在
woocommerce_email_order_details钩子前添加过滤器,仅针对当前已完成订单邮件的订单详情表格生效 - 仅移除支付方式:过滤器函数只删除
payment_method项,保留其他总计内容(如不需要移除购物车小计,可删除原代码中的unset($totals['cart_subtotal'])) - 及时移除过滤器:执行完订单详情后立即移除过滤器,防止影响后续的邮件钩子或其他WooCommerce功能
注意事项
- 确保模板文件放置在正确路径:
当前主题目录/woocommerce/emails/customer-completed-order.php - WooCommerce版本更新时,需检查官方模板是否有变动,必要时重新同步模板并再次添加修改
内容的提问来源于stack exchange,提问作者Ethan D
相关产品推荐
相关产品推荐

