Skip to content

Instantly share code, notes, and snippets.

@TanvirHasan19
Created January 6, 2026 04:44
Show Gist options
  • Select an option

  • Save TanvirHasan19/fdc77565c0a0b41cbb65a34218758c89 to your computer and use it in GitHub Desktop.

Select an option

Save TanvirHasan19/fdc77565c0a0b41cbb65a34218758c89 to your computer and use it in GitHub Desktop.
Push wholesale user data to Google Tag Manager data layer
/**
* Push wholesale user data to Google Tag Manager data layer
*/
function wws_push_user_data_to_datalayer() {
// Only run on frontend
if ( is_admin() ) {
return;
}
$is_logged_in = is_user_logged_in();
$is_wholesale = false;
$user_type = 'b2c';
// Check if user is wholesale
if ( $is_logged_in ) {
global $wc_wholesale_prices;
if ( $wc_wholesale_prices && isset( $wc_wholesale_prices->wwp_wholesale_roles ) ) {
$user_wholesale_role = $wc_wholesale_prices->wwp_wholesale_roles->getUserWholesaleRole();
if ( ! empty( $user_wholesale_role ) && is_array( $user_wholesale_role ) ) {
$is_wholesale = true;
$user_type = 'b2b';
}
}
}
?>
<script>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'logged_in': <?php echo $is_logged_in ? 'true' : 'false'; ?>,
'is_wholesale': <?php echo $is_wholesale ? 'true' : 'false'; ?>,
'user_type': '<?php echo esc_js( $user_type ); ?>'
});
</script>
<?php
}
add_action( 'wp_head', 'wws_push_user_data_to_datalayer', 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment