mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-04-29 11:32:21 +08:00
https://stackoverflow.com/questions/70795779/disable-default-country-in-woocommerce-if-geolocation-fails/70798677
20 lines
663 B
Text
20 lines
663 B
Text
function filter_default_checkout_billing_country( $default ) {
|
|
// If the user already exists, don't override country
|
|
if ( WC()->customer->get_is_paying_customer() ) {
|
|
return $default;
|
|
} elseif ( class_exists( 'WC_Geolocation' ) ) {
|
|
// Get location country
|
|
$location = WC_Geolocation::geolocate_ip();
|
|
|
|
if ( isset( $location['country'] ) ) {
|
|
return $location['country'];
|
|
} else {
|
|
$default = null;
|
|
}
|
|
} else {
|
|
$default = null;
|
|
}
|
|
|
|
return $default;
|
|
}
|
|
add_filter( 'default_checkout_billing_country', 'filter_default_checkout_billing_country', 10, 1 );
|