如何通过PHP移除WooCommerce客户订单完成邮件中的指定文本?
移除WooCommerce已完成订单邮件中的默认文本
方法一:移除默认钩子回调(完全替换前置内容)
如果你想完全替换customer_completed_order邮件中订单表格上方的默认文本,可通过移除WooCommerce挂载的默认回调函数实现。默认文本由woocommerce_email_order_details函数输出,该函数挂载在woocommerce_email_before_order_table钩子上,优先级为10。
将以下代码添加到你的主题functions.php文件或自定义插件中:
// 先移除默认的订单详情文本输出 add_action( 'woocommerce_email_before_order_table', 'remove_default_completed_order_text', 5 ); function remove_default_completed_order_text() { remove_action( 'woocommerce_email_before_order_table', 'woocommerce_email_order_details', 10 ); } // 保留你已有的自定义文本函数 add_action( 'woocommerce_email_before_order_table', 'fs_action_woocommerce_email_responder', 20, 4 ); function fs_action_woocommerce_email_responder( $order, $sent_to_admin, $plain_text, $email ) { // 这里是你的自定义替换文本 echo '<p>你的自定义内容</p>'; }
- 优先级5的动作确保在默认回调执行前将其移除,避免原文本输出。
- 你的自定义函数保持优先级20,正常输出替换内容。
方法二:过滤邮件内容(仅移除特定文本)
如果只想移除"We have finished processing your order."这句话,保留其他默认邮件内容,可使用邮件专属过滤器直接替换文本:
add_filter( 'woocommerce_email_customer_completed_order_content', 'remove_completed_order_target_text', 20, 2 ); function remove_completed_order_target_text( $content, $email ) { // 精准替换目标文本为空字符串 $content = str_replace( 'We have finished processing your order.', '', $content ); return $content; }
- 该过滤器仅作用于
customer_completed_order类型的邮件,不会影响其他邮件模板。 - 优先级20确保在WooCommerce生成邮件内容后进行替换操作。
内容的提问来源于stack exchange,提问作者AJD
相关产品推荐
相关产品推荐

