You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WooCommerce钩子woocommerce_email_order_details中查找修改自定义字符串

修改WooCommerce woocommerce_email_order_details 钩子输出的方法

你可以按照以下两种常用方案修改该钩子的输出内容,代码可添加到当前主题的functions.php文件或自定义功能插件中:

方案1:完全自定义输出内容(含原有内容保留选项)

该方案适合需要替换原有钩子自带的文本,同时可以选择是否保留后续订单详情内容:

  • 先移除WooCommerce默认挂载的回调函数
remove_action( 'woocommerce_email_order_details', 'wc_email_order_details', 10 );
  • 新增自定义回调函数输出你需要的内容
add_action( 'woocommerce_email_order_details', 'custom_email_order_details', 10, 4 );
function custom_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
    // 此处输出你要替换的自定义文本
    echo '<p style="margin: 0 0 20px 0;">你的自定义文本内容</p>';

    // 如需保留原有的订单详情展示,保留下方代码即可,不需要则删除
    wc_email_order_details( $order, $sent_to_admin, $plain_text, $email );
}

方案2:仅在原有内容前追加自定义文本

如果你不需要修改原有钩子的输出,只是要在问候语下方、原有订单详情上方加自定义内容,直接挂载更高优先级的回调即可:

add_action( 'woocommerce_email_order_details', 'add_custom_text_before_order_details', 5, 4 );
function add_custom_text_before_order_details( $order, $sent_to_admin, $plain_text, $email ) {
    // 可通过判断邮件ID,仅在指定类型的邮件中显示自定义内容,示例为仅处理中订单邮件生效
    if ( $email->id === 'customer_processing_order' ) {
        echo '<p>你要显示的自定义文本内容</p>';
    }
}

注意事项

  • 如果使用子主题,代码请添加到子主题的functions.php文件中,避免主主题更新覆盖修改
  • 修改完成后可通过新建测试订单触发邮件,验证修改是否生效

内容的提问来源于stack exchange,提问作者bobrock4

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 03:48:04