Last active
June 25, 2023 20:46
-
-
Save zackeryfretty/8bffe41ba2d1c41c94c1f3c4bc551270 to your computer and use it in GitHub Desktop.
Quick way to add a custom tracking script to the WooCommerce thank you / order confirmation page and prevent it from running multiple times in the event of a user page refresh.
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 // Don't Include | |
// Run Scripts on Order Confirmation Endpoint in WooCommerce Once. | |
function zf_woocommerce_thankyou_scripts($order_id) { | |
// Check for meta key on order, only run if not found. | |
if( ! get_post_meta( $order_id , '_tracking_scripts_ran', true ) ) { | |
// Add meta key to order so scripts don't run a second time. | |
add_post_meta( $order_id, '_tracking_scripts_ran', 'yes', true ); ?> | |
<script type="text/javascript"> | |
<!-- Your Tracking Script(s) here --> | |
</script> | |
<?php } | |
} | |
add_action( 'woocommerce_thankyou', 'zf_woocommerce_thankyou_scripts' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment