在收到的Woocommerce订单中的javascript中设置订单ID和订单总数

时间:2018-11-17 09:11:37

标签: javascript php wordpress woocommerce orders

下面有一个脚本,我确实集成到woocommerce的“谢谢”页面,我不知道要显示orderIDorderValue的内容,有人可以帮我吗?

(function() {
  function c(t) {
    for (var n = document.cookie.split(";"), r = 0; r < n.length; r++)
      if (p = n[r].split("="), p[0] == t)
        return p[1];
    return ''
  }
  adrecord = {
    "programID": 15,
    "orderID": "HERE I NEED TO SHOW orderID (order number)",
    "orderValue": HERE I NEED TO SHOW orderValue inc tax,
    "trackID": c('trackID'),
    "source": c('source')
  };
  var e = document.createElement('script');
  e.src = 'https://track.adrecord.com/track.js';
  var s = document.getElementsByTagName('script')[0];
  s.appendChild(e);
})();

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您可以在woocommerce_thankyou操作挂钩中使用以下挂钩函数,该挂钩函数将在Javascript中设置正确的订单ID和总数:

add_action( 'woocommerce_thankyou', 'thankyou_tracking_script', 10, 1 );
function thankyou_tracking_script( $order_id ) {
    // Get the WC_Order instance Object
    $order = wc_get_order( $order_id );

    // Output Javascript
    ?>
    <!-- JS Order received Script -->
    <script type="text/javascript"> 
    (function() {
        function c(t) {
            for (var n = document.cookie.split(";"), r = 0; r < n.length; r++)
            if (p = n[r].split("="), p[0] == t) return p[1];
            return ''
        }
        adrecord = {
            "programID": 15,
            "orderID": "<?php echo $order_id; ?>",
            "orderValue": <?php echo $order->get_total(); ?>,
            "trackID": c('trackID'),
            "source": c('source')
        };
        var e = document.createElement('script');
        e.src = 'https://track.adrecord.com/track.js';
        var s = document.getElementsByTagName('script')[0];
        s.appendChild(e);
    })(); 
    </script>
    <?php
}

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。


或使用wp_footer动作钩子定位到“收到订单” (谢谢)页面的类似代码:

add_action( 'wp_footer', 'order_received_tracking_script' );
function order_received_tracking_script() {
    // Only on "Order received" page
    if( ! is_wc_endpoint_url('order-received') ) Return; // Exit

    $order_id  = absint( $wp->query_vars['order-received'] );

    // Get the WC_Order instance Object
    $order = wc_get_order( $order_id );

    // Output Javascript
    ?>
    <!-- JS Order received Script -->
    <script type="text/javascript"> 
    (function() {
        function c(t) {
            for (var n = document.cookie.split(";"), r = 0; r < n.length; r++)
            if (p = n[r].split("="), p[0] == t) return p[1];
            return ''
        }
        adrecord = {
            "programID": 15,
            "orderID": "<?php echo $order_id; ?>",
            "orderValue": <?php echo $order->get_total(); ?>,
            "trackID": c('trackID'),
            "source": c('source')
        };
        var e = document.createElement('script');
        e.src = 'https://track.adrecord.com/track.js';
        var s = document.getElementsByTagName('script')[0];
        s.appendChild(e);
    })(); 
    </script>
    <?php
}

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。