Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nilsbosman/c019f4954765b2b5587f44495c61fffb to your computer and use it in GitHub Desktop.
Save nilsbosman/c019f4954765b2b5587f44495c61fffb to your computer and use it in GitHub Desktop.
Custom tracking code for the thanks page (only triggers once per order)
<?php
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();
}
}
@nilsbosman
Copy link
Author

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