将Trustpilot JS嵌入WooCommerce订单处理邮件 现有代码无效求解决
WooCommerce邮件内嵌Trustpilot脚本不生效解决方案
核心问题原因
- 绝大多数邮件客户端会默认拦截所有
<script>标签内容,无论脚本MIME类型是什么,你嵌入的JSON脚本会被直接过滤,无法被Trustpilot识别,这是最主要的失效原因。 - SKU拼接逻辑错误:当前代码输出的
productSkus字段结构为["sku1,sku2,sku3"],不符合Trustpilot要求的多SKU结构["sku1","sku2","sku3"]。 - 未限制邮件触发类型:你挂载的
woocommerce_email_header钩子会在所有WooCommerce邮件中插入脚本,没有限定只在客户订单处理邮件中生效。
推荐解决方案
优先使用链接参数拼接方案替代邮件内嵌脚本,避免被邮件客户端过滤:
你可以将订单参数直接拼接在Trustpilot的评价邀请链接中,插入到邮件正文/底部即可,参数格式可直接匹配Trustpilot的自定义字段要求。
代码逻辑修正(若仍需使用脚本方案)
如果你需要在支持脚本解析的场景(比如网站订单确认页,而非邮件)使用该功能,修正后的代码如下,使用json_encode自动处理JSON格式和转义,避免手动拼接出错:
// 如果是用在网站订单页,换钩子为 woocommerce_thankyou add_action ('woocommerce_email_header', 'add_trustpilot_script', 9999, 3); function add_trustpilot_script ($headers, $email_id, $order ){ // 仅在客户订单处理邮件中生效 if($email_id !== 'customer_processing_order') return; $skuList = []; foreach ( $order->get_items() as $item_id => $item ) { $product = $item->get_product(); if($product){ $skuList[] = $product->get_sku(); } } $trustpilotData = [ 'recipientName' => $order->get_billing_first_name(), 'recipientEmail' => $order->get_billing_email(), 'referenceId' => $order->get_id(), 'productSkus' => $skuList ]; echo '<script type="application/json+trustpilot">' . json_encode($trustpilotData, JSON_UNESCAPED_UNICODE) . '</script>'; }
内容的提问来源于stack exchange,提问作者AdrianV
相关产品推荐
相关产品推荐

