Skip to content

Instantly share code, notes, and snippets.

@davevanhoorn
Created January 17, 2021 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davevanhoorn/22cac2d1f199e0565b11fff82986b4ad to your computer and use it in GitHub Desktop.
Save davevanhoorn/22cac2d1f199e0565b11fff82986b4ad to your computer and use it in GitHub Desktop.
WooCommerce action thankyou.php and functions.php
/**
* file: functions.php
* Update meta data when someone's looking at their order thank you page
*/
function thank_you_page( $order_id ){
$order = wc_get_order($order_id);
$order->update_meta_data('_user_has_loaded_thank_you_page', 'yes');
$order->save();
}
add_action( 'woocommerce_thankyou', __NAMESPACE__ . '\\thank_you_page', 10, 1);
/**
* file: thankyou.php
* Push to the dataLayer only when order meta data is not "yes"
*/
<?php if ('yes' !== get_post_meta($order->get_id(), '_user_has_loaded_thank_you_page', true)) { ?>
<script>
window.dataLayer = window.dataLayer || [];
........
</script>
<?php } ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment