Embed clickwork7 tracking code with the transactio

2019-08-20 09:12发布

问题:

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:

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:

  • Add Order data in Adword Conversion Code in Woocommerce
  • Linkwise Affiliate integration in Woocommerce thankyou page