Last active
October 13, 2024 07:29
-
-
Save emre-edu-tech/4ce4adac5e6a5db72932c5bfc5024e10 to your computer and use it in GitHub Desktop.
Check the user's country and show them as a warning using SweetAlert2. Warning was shown on Checkout page if the country is other than Germany in this example.
This file contains 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 src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script> | |
<script> | |
Swal.fire({ | |
title: 'Achtung!', | |
html: 'Ihr Land: <?php echo WC()->countries->countries[ $country ] ?>!<br>Wir versenden nur innerhalb Deutschland.', | |
icon: 'warning', | |
}) | |
</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