Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Frontend Snippets] Custom tracking code for the thanks page
/**
* Add custom tracking code to the thank-you page
*/
add_action( 'woocommerce_thankyou', 'my_custom_tracking' );
function my_custom_tracking( $order_id ) {
// Lets grab the order
$order = wc_get_order( $order_id );
/**
* Put your tracking code here
* You can get the order total etc e.g. $order->get_total();
*/
// This is the order total
$order->get_total();
// This is how to grab line items from the order
$line_items = $order->get_items();
// This loops over line items
foreach ( $line_items as $item ) {
// This will be a product
$product = $order->get_product_from_item( $item );
// This is the products SKU
$sku = $product->get_sku();
// This is the qty purchased
$qty = $item['qty'];
// Line item total cost including taxes and rounded
$total = $order->get_line_total( $item, true, true );
// Line item subtotal (before discounts)
$subtotal = $order->get_line_subtotal( $item, true, true );
}
}
@nilsbosman

This comment has been minimized.

Copy link

@nilsbosman nilsbosman commented Jan 28, 2020

This will fire every time the thank you page is visited. So if the customer revisits or refreshes the page this code will run again. Is there an alternative that only fires once when the order is made/completed?

@oshri-humanz

This comment has been minimized.

Copy link

@oshri-humanz oshri-humanz commented May 24, 2021

@nilsbosman did you resolved the issue? we have the same problem

@nilsbosman

This comment has been minimized.

Copy link

@nilsbosman nilsbosman commented Jun 2, 2021

@oshri-humanz I've fixed this issue by adding meta_data to the order after it has been processed once. When the same order thank you page is opened it will check for the meta_data and if it exists it will skip the code. Also added a filter to exclude localhost/dev submits.

add_action( 'woocommerce_thankyou', 'tracking_code', 10, 1 );

function tracking_code( $order_id ) {

    if ( ! $order_id )
        return;

    // Ignore localhost/dev submits
    $localhosts = array(
        '127.0.0.1',
        '::1'
    );

    // Allow code execution only once per order.
    if( !get_post_meta($order_id, '_thankyou_action_done', true) && !in_array($_SERVER['REMOTE_ADDR'], $localhosts) ) {

        // Lets grab the order
        $order = wc_get_order( $order_id );

        // DO ALL YOUR TRACKING STUFF HERE

        // Flag the action as done (to avoid repetitions on reload for example)
        $order->update_meta_data( '_thankyou_action_done', true );
        $order->save();
    }
}
@tarsonis123

This comment has been minimized.

Copy link

@tarsonis123 tarsonis123 commented Jun 5, 2021

@nilsbosman Hey there thanks for uploading your solution! Would you mind to explain a bit further how you added the the meta_data to the orders or hint us to a documentation about that?
Does this code actually work for a custom thank-you page or just the default woo one?

@nilsbosman

This comment has been minimized.

Copy link

@nilsbosman nilsbosman commented Jun 7, 2021

@tarsonis123 Hi, for sure.
You can find the documentation for adding meta data to orders in WooCommerce here. The first time you visit the thank-you page it wil check if the meta data exists, it doens't since it's the first time it's been openend. It will run the tracking code part and add the meta data. The second time you visit the thank-you page it will check if the meta exists again. This time it does exists since it's already been visited and skips the tracking code part.

It uses the woocommerce_thankyou action that is part of the woocommerce thank-you page template, see here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment