Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

javascript - Embed clickwork7 tracking code with the transaction ID in Woocommerce

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...