Add the below code at the end of your theme’s functions.php file or you can also add the below code inside your site-specific plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | /** * 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 ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.