Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zackeryfretty/8bffe41ba2d1c41c94c1f3c4bc551270 to your computer and use it in GitHub Desktop.
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.
<?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