Automattic\WooCommerce\Internal\ProductCustoms

CustomsDataValidator::normalize_country_of_origin │ public static │ WC 11.3.0

Normalizes an origin country against the full WooCommerce country list.

Метод класса: CustomsDataValidator{}

Хуков нет.

Возвращает

string|null.

Использование

$result = CustomsDataValidator::normalize_country_of_origin( $value ): ?string;
$value(разное) (обязательный)
Country code, or null to clear it.

Список изменений

С версии 11.3.0 Введена.

Код CustomsDataValidator::normalize_country_of_origin() WC 11.3.0

public static function normalize_country_of_origin( $value ): ?string {
	if ( null === $value ) {
		return null;
	}

	// Unicode-aware trim, so a pasted non-breaking space counts as empty. Null means invalid UTF-8.
	$trimmed = is_string( $value ) ? preg_replace( '/^\s+|\s+$/u', '', $value ) : null;
	if ( null !== $trimmed ) {
		$code = strtoupper( $trimmed );
		if ( '' === $code ) {
			return null;
		}
		// WC()->countries is only set once WooCommerce has initialized.
		$countries = WC()->countries instanceof WC_Countries ? WC()->countries : new WC_Countries();
		if ( $countries->country_exists( $code ) ) {
			return $code;
		}
	}

	throw new WC_Data_Exception(
		'woocommerce_product_invalid_customs_country_of_origin',
		__( 'Select a valid country of origin.', 'woocommerce' ), // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Escaped when output.
		400
	);
}