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

如何在Woocommerce发送的BCC邮件中嵌入Trustpilot结构化脚本

WooCommerce新订单BCC邮件嵌入Trustpilot结构化脚本方案

你原来的代码仅实现了在新订单邮件头添加BCC收件人的功能,要在邮件内容中嵌入Trustpilot结构化脚本,需要额外调用WooCommerce的邮件内容过滤钩子,同时填充脚本所需的动态字段即可,完整实现代码如下:

// 原有功能:新订单邮件添加BCC收件人
add_filter( 'woocommerce_email_headers', 'firefog_headers_filter_function', 10, 3);
function firefog_headers_filter_function( $headers, $email_id, $order ) {
    if ($email_id == 'new_order') {
        $headers .= 'BCC: NAME <name@domain.com>' . "\r\n";
    }
    return $headers;
}

// 新增功能:新订单邮件内容嵌入Trustpilot结构化脚本
add_filter( 'woocommerce_mail_content', 'firefog_add_trustpilot_script_to_email', 10, 2 );
function firefog_add_trustpilot_script_to_email( $content, $email ) {
    // 仅在新订单邮件中注入脚本
    if ( $email->id !== 'new_order' || empty( $email->object ) ) {
        return $content;
    }

    $order = $email->object;
    // 填充动态参数,可根据实际需求调整取值
    $trustpilot_script = sprintf(
        '<script type="application/json+trustpilot">
            {
                "recipientName": "%s",
                "recipientEmail": "%s",
                "referenceId": "%s",
                "locale": "%s"
            }
        </script>',
        esc_js( $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() ),
        esc_js( $order->get_billing_email() ),
        esc_js( $order->get_id() ),
        esc_js( get_locale() )
    );

    // 脚本追加到邮件内容末尾
    return $content . $trustpilot_script;
}

注意事项

  • 调整了原有header钩子的参数数量,从2个改为3个,兼容更高版本WooCommerce的参数规则
  • 动态字段的取值可根据你的业务需求修改,比如locale可以替换为订单对应的用户语言
  • 结构化脚本属于邮件隐藏内容,不会对普通用户的邮件阅读体验产生影响,Trustpilot服务会自动识别对应格式的脚本提取信息

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 22:39:00