WC_Countries::get_country_from_alpha_3_code
Searches for a valid ISO 3166-1 alpha-2 code using the provided alpha-3 code.
Method of the class: WC_Countries{}
No Hooks.
Returns
String|null. The alpha-2 country code, or null if not found.
Usage
$WC_Countries = new WC_Countries(); $WC_Countries->get_country_from_alpha_3_code( $country_code );
- $country_code(string) (required)
- The alpha-3 country code to search for.
Changelog
| Since 10.3.0 | Introduced. |
WC_Countries::get_country_from_alpha_3_code() WC Countries::get country from alpha 3 code code WC 10.8.1
public function get_country_from_alpha_3_code( $country_code ) {
// Validate input.
if ( empty( $country_code ) || ! is_string( $country_code ) ) {
return null;
}
try {
$data = ( new Automattic\WooCommerce\Vendor\League\ISO3166\ISO3166() )->alpha3( $country_code );
if ( ! isset( $data['alpha2'] ) ) {
throw new \Exception( 'Alpha-2 country code not found for alpha-3 code.' );
}
// Return the alpha-2 code.
return $data['alpha2'];
} catch ( \Exception $e ) {
return null;
}
}