WC_Countries::get_formatted_address( array $args = array(), string $separator = '<br/>' )
Get country address format.
Description Description
Parameters Parameters
- $args
-
(Optional) Arguments.
Default value: array()
- $separator
-
(Optional) How to separate address lines. @since 3.5.0.
Default value: '<br/>'
Return Return
(string)
Source Source
File: includes/class-wc-countries.php
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}' => $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( $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;
}