Last active
June 7, 2021 13:39
-
-
Save nilsbosman/c019f4954765b2b5587f44495c61fffb to your computer and use it in GitHub Desktop.
Custom tracking code for the thanks page (only triggers once per order)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fix for this issue: https://gist.github.com/woogists/955db81b57a5bf6f7d88fefffef2fa85#gistcomment-3158550