Skip to content

Instantly share code, notes, and snippets.

@emre-edu-tech
Created November 21, 2023 09:43
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 emre-edu-tech/fb68179c7edb0c00fdc618ea360b1a0e to your computer and use it in GitHub Desktop.
Save emre-edu-tech/fb68179c7edb0c00fdc618ea360b1a0e to your computer and use it in GitHub Desktop.
Wocoommerce - Detect user Country on Checkout Page
<?php
function get_user_geo_country(){
if(is_checkout()) {
$geo = new WC_Geolocation(); // Get WC_Geolocation instance object
$user_ip = $geo->get_ip_address(); // Get user IP
$user_geo = $geo->geolocate_ip( $user_ip ); // Get geolocated user data.
$country = $user_geo['country']; // Get the country code
if($country != 'DE') {
?>
<script>
alert('Ihr Land: <?php echo WC()->countries->countries[ $country ] ?>!<br>Wir versenden nur innerhalb Deutschland.')
</script>
<?php
}
}
}
add_action('wp_footer', 'get_user_geo_country');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment