douyue1481 2018-08-09 16:35
浏览 73
已采纳

在Woocommerce中使用事务ID嵌入clickwork7跟踪代码

In Woocommerce, I was looking to get order ID just before payment, when the order is created. I found this answer below:
Get the order ID in checkout page before payment process

What I need is to pass in tracking script the transaction ID (as specified on the script) and I should be able to track in clickwork7 dashboard:

<script type="text/javascript" src="https://clickwork7secure.com/p.ashx?
o=45875&e=12995&f=js&t=TRANSACTION_ID"></script>

But the transaction ID seems to be empty after a purchase in Paypal for example, so may be I should pass the order ID instead.

The Order received page seems to be the right place, but for cancelled or failed orders, Where and how to embed this script?

Any help is appreciated.

  • 写回答

1条回答 默认 最新

  • dqrsceg6279196 2018-08-09 16:45
    关注

    Updated: It is possible to use many different hooks for that:

    • wp_head
    • wp_footer
    • woocommerce_thankyou

    You can try to use the:

    • Order id (Is easy to get it)
    • Order Key: $order_key = get_post_meta( $order_id, '_order_key', true );
    • Transaction id: $transaction_id = get_post_meta( $order_id, '_transaction_id', true );

    1) Using woocommerce_thankyou hook: The simpler way as the Order Id is a hook argument:

    add_action( 'woocommerce_thankyou', 'checkout_clickwork_js_script', 22, 1 );
    function checkout_clickwork_js_script( $order_id ) {
        if (  ! $order_id  ) return; // Exit
    
        $transaction_id = get_post_meta( $order_id, '_transaction_id', true );
        $order_key      = get_post_meta( $order_id, '_order_key', true );
    
        if( ! empty($transaction_id) ){
            $value = $transaction_id; // TRANSACTION ID
        }
        elseif( ! empty($order_key) ){
            $value = $transaction_id; // ORDER KEY
        } 
        else { 
            $value = $transaction_id; // ORDER ID
    
        $url = "https://clickwork7secure.com/p.ashx?o=45875&e=12995&f=js&t=$value";
    
        ?><script type="text/javascript" src="<?php echo $url; ?>"></script> <?php
    }
    

    Code goes in function.php file of your active child theme (or active theme). It should work.


    2) Using wp_head hook:

    add_action( 'wp_head', 'checkout_clickwork_js_script', 998 );
    function checkout_clickwork_js_script() {
        // Only order-received page 
        if( is_wc_endpoint_url('order-received') ) :
    
        global $wp;
    
        $order_id  = absint( $wp->query_vars['order-received'] );
    
        if ( ! $order_id || empty($order_id) )
            return; // Exit
    
        $transaction_id = get_post_meta( $order_id, '_transaction_id', true );
        $order_key      = get_post_meta( $order_id, '_order_key', true );
    
        if( ! empty($transaction_id) ){
            $value = $transaction_id; // TRANSACTION ID
        }
        elseif( ! empty($order_key) ){
            $value = $transaction_id; // ORDER KEY
        } 
        else { 
            $value = $transaction_id; // ORDER ID
    
        $url = "https://clickwork7secure.com/p.ashx?o=45875&e=12995&f=js&t=$value";
    
        ?><script type="text/javascript" src="<?php echo $url; ?>"></script> <?php
        endif;
    }
    

    Code goes in function.php file of your active child theme (or active theme). It should work.

    The condition if( is_wc_endpoint_url('order-received') ) : can be extended to handle cancelled and failed orders custom endpoints too …


    Similar answers:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加