Created
November 21, 2023 09:43
-
-
Save emre-edu-tech/fb68179c7edb0c00fdc618ea360b1a0e to your computer and use it in GitHub Desktop.
Wocoommerce - Detect user Country on Checkout Page
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 | |
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