WC_Integration_MaxMind_Database_Service::get_iso_country_code_for_ip( string $ip_address )

Fetches the ISO country code associated with an IP address.


Description Description


Parameters Parameters

$ip_address

(Required) The IP address to find the country code for.


Top ↑

Return Return

(string) The country code for the IP address, or empty if not found.


Top ↑

Source Source

File: includes/integrations/maxmind-geolocation/class-wc-integration-maxmind-database-service.php

	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;
	}


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.