WC_Integration_MaxMind_Database_Service::get_iso_country_code_for_ip
Fetches the ISO country code associated with an IP address.
Method of the class: WC_Integration_MaxMind_Database_Service{}
No Hooks.
Returns
String. The country code for the IP address, or empty if not found.
Usage
$WC_Integration_MaxMind_Database_Service = new WC_Integration_MaxMind_Database_Service(); $WC_Integration_MaxMind_Database_Service->get_iso_country_code_for_ip( $ip_address );
- $ip_address(string) (required)
- The IP address to find the country code for.
WC_Integration_MaxMind_Database_Service::get_iso_country_code_for_ip() WC Integration MaxMind Database Service::get iso country code for ip code WC 10.8.1
public function get_iso_country_code_for_ip( $ip_address ) {
$country_code = '';
if ( ! class_exists( 'MaxMind\Db\Reader' ) ) {
wc_get_logger()->notice( __( 'Missing MaxMind Reader library!', 'woocommerce' ), array( 'source' => 'maxmind-geolocation' ) );
return $country_code;
}
$database_path = $this->get_database_path();
if ( ! file_exists( $database_path ) ) {
return $country_code;
}
try {
$reader = new MaxMind\Db\Reader( $database_path );
$data = $reader->get( $ip_address );
if ( isset( $data['country']['iso_code'] ) ) {
$country_code = $data['country']['iso_code'];
}
$reader->close();
} catch ( Exception $e ) {
wc_get_logger()->notice( $e->getMessage(), array( 'source' => 'maxmind-geolocation' ) );
}
return $country_code;
}