WC_Countries::get_formatted_address()publicWC 1.0

Get country address format.

Method of the class: WC_Countries{}

Return

String.

Usage

$WC_Countries = new WC_Countries();
$WC_Countries->get_formatted_address( $args, $separator );
$args(array)
Arguments.
Default: array()
$separator(string)
How to separate address lines. @since 3.5.0.
Default: '<br/>'

WC_Countries::get_formatted_address() code WC 9.4.2

public function get_formatted_address( $args = array(), $separator = '<br/>' ) {
	$default_args = array(
		'first_name' => '',
		'last_name'  => '',
		'company'    => '',
		'address_1'  => '',
		'address_2'  => '',
		'city'       => '',
		'state'      => '',
		'postcode'   => '',
		'country'    => '',
	);

	$args    = array_map( 'trim', wp_parse_args( $args, $default_args ) );
	$state   = $args['state'];
	$country = $args['country'];

	// Get all formats.
	$formats = $this->get_address_formats();

	// Get format for the address' country.
	$format = ( $country && isset( $formats[ $country ] ) ) ? $formats[ $country ] : $formats['default'];

	// Handle full country name.
	$full_country = ( isset( $this->countries[ $country ] ) ) ? $this->countries[ $country ] : $country;

	// Country is not needed if the same as base.
	if ( $country === $this->get_base_country() && ! apply_filters( 'woocommerce_formatted_address_force_country_display', false ) ) {
		$format = str_replace( '{country}', '', $format );
	}

	// Handle full state name.
	$full_state = ( $country && $state && isset( $this->states[ $country ][ $state ] ) ) ? $this->states[ $country ][ $state ] : $state;

	// Substitute address parts into the string.
	$replace = array_map(
		'esc_html',
		apply_filters(
			'woocommerce_formatted_address_replacements',
			array(
				'{first_name}'       => $args['first_name'],
				'{last_name}'        => $args['last_name'],
				'{name}'             => sprintf(
					/* translators: 1: first name 2: last name */
					_x( '%1$s %2$s', 'full name', 'woocommerce' ),
					$args['first_name'],
					$args['last_name']
				),
				'{company}'          => $args['company'],
				'{address_1}'        => $args['address_1'],
				'{address_2}'        => $args['address_2'],
				'{city}'             => $args['city'],
				'{state}'            => $full_state,
				'{postcode}'         => $args['postcode'],
				'{country}'          => $full_country,
				'{first_name_upper}' => wc_strtoupper( $args['first_name'] ),
				'{last_name_upper}'  => wc_strtoupper( $args['last_name'] ),
				'{name_upper}'       => wc_strtoupper(
					sprintf(
						/* translators: 1: first name 2: last name */
						_x( '%1$s %2$s', 'full name', 'woocommerce' ),
						$args['first_name'],
						$args['last_name']
					)
				),
				'{company_upper}'    => wc_strtoupper( $args['company'] ),
				'{address_1_upper}'  => wc_strtoupper( $args['address_1'] ),
				'{address_2_upper}'  => wc_strtoupper( $args['address_2'] ),
				'{city_upper}'       => wc_strtoupper( $args['city'] ),
				'{state_upper}'      => wc_strtoupper( $full_state ),
				'{state_code}'       => wc_strtoupper( $state ),
				'{postcode_upper}'   => wc_strtoupper( $args['postcode'] ),
				'{country_upper}'    => wc_strtoupper( $full_country ),
			),
			$args
		)
	);

	$formatted_address = str_replace( array_keys( $replace ), $replace, $format );

	// Clean up white space.
	$formatted_address = preg_replace( '/  +/', ' ', trim( $formatted_address ) );
	$formatted_address = preg_replace( '/\n\n+/', "\n", $formatted_address );

	// Break newlines apart and remove empty lines/trim commas and white space.
	$formatted_address = array_filter( array_map( array( $this, 'trim_formatted_address_line' ), explode( "\n", $formatted_address ) ) );

	// Add html breaks.
	$formatted_address = implode( $separator, $formatted_address );

	// We're done!
	return $formatted_address;
}