【问题标题】:Hook JS into WooCommerce Email function将 JS 挂钩到 WooCommerce 电子邮件功能
【发布时间】:2023-03-16 20:07:01
【问题描述】:

我正在尝试在我的 WooCommerce 处理订单电子邮件 (customer-processing-order.php) 中为 Trustpilot 添加一些 JS。我目前正在尝试使用 functions.php 中的函数来回显它,如下所示:

add_action ('woocommerce_email_header', 'add_trustpilot_script', 9999, 3);

function add_trustpilot_script ($headers, $email_id, $order ){
$theSku = '';
foreach ( $order->get_items() as $item_id => $item ) {
          $sku = $item->get_sku();
          $theSku .= $sku . ',';
}
$tSku = substr($theSku, 0,-1);

echo '<script type="application/json+trustpilot">
   { 
      "recipientName": "'. $order->get_billing_first_name().'", 
      "recipientEmail": "'. $order->get_billing_email().'", 
      "referenceId": "'. $order->get_id().'", 
      "productSkus": ["'.$tSku.'"]
   }
</script>';
}

但上述方法不起作用:(感谢任何帮助。

【问题讨论】:

  • 你应该把这个放到感谢页面,不确定你是否可以在电子邮件中输入脚本

标签: php wordpress woocommerce


【解决方案1】:

您的代码包含一些小错误

  • $order 不是 woocommerce_email_header 钩子中的参数
  • $email-&gt;id 用于 if 条件,以定位 customer_processing_order 电子邮件
  • $item-&gt;get_sku(); 替换为 $product-&gt;get_sku();

所以你得到:

function action_woocommerce_email_header( $email_heading, $email ) {    
    // Only for order processing email 
    if ( $email->id == 'customer_processing_order' ) {  
        // Get an instance of the WC_Order object
        $order = $email->object;
        
        // Is a WC_Order
        if ( is_a( $order, 'WC_Order' ) ) {
            // Empty string
            $theSku = '';
            
            foreach ( $order->get_items() as $item_id => $item ) {
                // Get an instance of corresponding the WC_Product object
                $product = $item->get_product();
                
                // Get product SKU
                $product_sku = $product->get_sku();
                
                $theSku .= $product_sku . ',';
            }
            
            $tSku = substr( $theSku, 0, -1 );

            echo '<script type="application/json+trustpilot">
            { 
                "recipientName": "' . $order->get_billing_first_name() . '",
                "recipientEmail": "' . $order->get_billing_email() . '", 
                "referenceId": "' . $order->get_id() . '", 
                "productSkus": ["' . $tSku . '"]
            }
            </script>';
        }
    }
} 
add_action( 'woocommerce_email_header', 'action_woocommerce_email_header', 10, 2 );

注意:结果可以在邮件源中找到,一个邮件客户端可视化显示结果,另一个不显示,所以这取决于你使用哪个邮件客户端

【讨论】:

  • 您知道如何在模板文件 (customer-processing-order.php) 中获取订单详细信息吗?我想我想通了,但我需要此模板中的订单详细信息
  • @AdrianV WooCommerce 中的电子邮件从不包含 1 个特定的模板文件。模板文件可以被覆盖,但不建议这样做,因此您可以使用挂钩。通过钩子,您可以在电子邮件中编辑/添加数据,但某些钩子在不同的模板文件中出现不止一次。这就是为什么您可以通过 if 条件(如我的回答中所应用的那样)定位特定电子邮件的原因,无论您是要修改模板文件,还是通过带有 if 条件的钩子来完成,都归结为同一件事。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
  • 2017-01-24
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
相关资源
最近更新 更多