I get asked this question quite a bit, and that is probably because this is something that every eCommerce store needs. The question usually goes like this:
How can I add my tracking code to the WooCommerce thank-you / order-recieved page?
There are two ways to tackle this. The easiest being to use a plugin. While there are quite a few plugins out there to do this, I recommended this plugin as it is created by the developers of WooCommerce: WooCommerce Google Analytics Integration
However, there is always the DIY crowd (me included) that like to do this on their own, or perhaps you are using a custom analytics system that requires you to give it a little special attention. In either case, you need to be able to put some tracking code on the page users go to after checking out so you can track the orders. And I’m sure you’ve noticed by now that WooCommerce does not have a specific page for this. Rather it is just the checkout page with a custom endpoint. So to add something specifically to this page, we will need to hook onto a hook found on just the thank-you page.
For this, we will be using the
woocommerce_thankyou hook found in the checkout/thankyou.php template.
tl;dr – the completed snippet will look like this:
About this snippet
Then, if the customer is on the order received page, we are adding the tracking code to the bottom of the page right where that hook is located in the template. Now you can add in your custom tracking code between the two commented lines, and you’ll be all set! I included some variables that you might need to use in your tracking code, such as the order ID and order Total. If you need some order information that is not there, take a peek at this file in WooCommerce and see if there is a function available that will give you the data you need from the order. Remember, that if you want to use one of those variables, you need to enter into PHP mode again like this:
<?php echo $total ?>
This snippet should go in your child theme’s functions.php file. If you are using a premium child theme (meaning your child theme will get updates from the theme authors), then you should use the Theme Customizations plugin.