Automattic\WooCommerce\StoreApi\Utilities

ValidationUtils::format_state()publicWC 1.0

Format a state based on the country. If country has defined states, will return a valid upper case state code.

Method of the class: ValidationUtils{}

No Hooks.

Return

String.

Usage

$ValidationUtils = new ValidationUtils();
$ValidationUtils->format_state( $state, $country );
$state(string) (required)
State name or code (sanitized).
$country(string) (required)
Country code.

ValidationUtils::format_state() code WC 9.4.2

public function format_state( $state, $country ) {
	$states = $this->get_states_for_country( $country );

	if ( count( $states ) ) {
		$state        = \wc_strtoupper( $state );
		$state_values = array_map( '\wc_strtoupper', array_flip( array_map( '\wc_strtoupper', $states ) ) );

		if ( isset( $state_values[ $state ] ) ) {
			// Convert to state code if a state name was provided.
			return $state_values[ $state ];
		}
	}

	return $state;
}