3

I'm trying to put order id and value to tracking code but can't make it work. I tried to put there

$order_id = $order->get_id();
$order->get_id();
$order->get_total();

and nothing works. Here's code and places where I need these elements:

<script language="JavaScript">
tdconv('init', '2253741', {'element': 'iframe' });
tdconv('track', 'sale', {'transactionId':'ORDER ID HERE', 'ordervalue':ORDER VALUE HERE, 'currency':'PLN', 'event':400532});
</script>

Can anyone give me some tips?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Kuba A2A
  • 31
  • 2
  • Put more code. First thing on mind is `init`'s `{'element': 'iframe' }`. What iframe? :) – ΔO 'delta zero' Jul 08 '20 at 12:49
  • @ΔO'deltazero' to be honest I'm not a developer, just shop administrator and I don't know much about technicals but that's a Tradedoubler tracking code on "thank you" page – Kuba A2A Jul 08 '20 at 12:50
  • Then better ask your developer :) Or consider implementing Google Tag Manager (tagmanager.google.com) or similar for easy tracking codes management without further code implementation requirements. – ΔO 'delta zero' Jul 08 '20 at 12:54
  • Related: [PHP calculate days from today for Google Survey Opt-In Code](https://stackoverflow.com/a/61944435/11987538), this is more or less the same, you can by example see how the order id is applied in javascript code. – 7uc1f3r Jul 08 '20 at 13:49
  • @7uc1f3r that worked! thank you so much :) – Kuba A2A Jul 08 '20 at 14:08
  • @KubaA2A nice to hear, please consider reading [What is voting up?](https://stackoverflow.com/help/privileges/vote-up) - Whenever you encounter a question, answer or comment that you feel is especially useful, vote it up! – 7uc1f3r Jul 08 '20 at 14:10
  • @KubaA2A The below answer based on [*Tracking Add too cart & submit Order for Facebook Pixel In WooCommerce*](https://stackoverflow.com/a/53892514/3730754) and show you how to get order details on Order received page for your Javascript tracking code. – LoicTheAztec Jul 08 '20 at 14:41

1 Answers1

0

Here is the way to get order data for a javascript tracking script from order received page (thankyou):

add_action( 'wp_footer', 'thankyou_tracking_script_js' );
function thankyou_tracking_script_js(){
    // Order reveived / thankyou page
    if ( ! is_wc_endpoint_url( 'order-received' ) ) return;

    global $wp;

    // If order_id is defined
    if ( isset($wp->query_vars['order-received']) && absint($wp->query_vars['order-received']) > 0 ) :
    
    $order_id       = absint($wp->query_vars['order-received']); // The order ID
    $order          = wc_get_order( $order_id ); // The WC_Order object
    $transaction_id = empty($order->get_transaction_id()) ? $order_id : $order->get_transaction_id(); // The transaction ID
    
    ?>
    <script language="JavaScript">
    tdconv('init', '2253741', {'element': 'iframe' });
    tdconv('track', 'sale', {
        'transactionId' : '<?php echo $transaction_id; ?>', 
        'ordervalue'    : '<?php echo $order->get_total(); ?>', 
        'currency'      : '<?php echo $order->get_order_currency(); ?>', 
        'event'         : 400532
    });
    </script>
    <?php
    endif;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Related:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399