E-commerce tracking for WooCommerce

Categories
Guide

In order to add a tracking code to the Thank You page in WooCommerce, you need to use the woocommerce_thankyou hook. First, you need to add a code that receives the order information and then outputs a script with the necessary data.

E-commerce tracking for WooCommerce - 0001

Create a new file in your theme (or child theme) called thankyou-tracking-code.php and add the following code there:
<?php
function add_thankyou_tracking_code( $order_id ) {
if ( ! $order_id ) {
 return;
 }
$order = wc_get_order( $order_id );
$quantity = 0;
$items = $order->get_items();
foreach ( $items as $item ) {
$quantity += $item->get_quantity();
}
?>
<script type="text/javascript" defer>
var plerdysendData = {
'type': 'commerce',
'data': {
'order_id': <?php echo $order->get_order_number(); ?>,
'money': <?php echo $order->get_total(); ?>,
'quantity': <?php echo $quantity; ?>
}
};
</script>
<?php
}
add_action( 'woocommerce_thankyou', 'add_thankyou_tracking_code' );

After that, include this file in your functions.php:

require_once get_template_directory() . '/thankyou-tracking-code.php';

E-commerce tracking for WooCommerce - 0002

Was this helpful?

Leave a reply for "E-commerce tracking for WooCommerce"

Your email address will not be published. Required fields are marked *