Custom tracking code for the thank-you page in WooCommerce

This snippet is for adding tracking for a separate piece of analytic software you may have installed. The order object contains all the order data you should need, including line items.

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme.

 * 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
	// 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 );
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Oopps, sorry. This discussion is closed.

If you are a paying customer and you are looking for Tech Support please post a ticket on our Support Tickets page and we will respond promptly.

If you are not a paying customer and you would like to post a pre-sale question or a quote request please use our Contact form.

Why did we close the discussion?

Many of the customers started reporting issues as comments instead of the ticketing system and this started delaying our response time.

We are making this change to serve our customers better. We have ramped up our support team and we are trying to get the reply out on the same day.

Thanks for understanding.