WC_Countries::get_address_fields( mixed $country = '', string $type = 'billing_' )
Apply locale and get address fields.
Description Description
Parameters Parameters
- $country
-
(Optional) Country.
Default value: ''
- $type
-
(Optional) Address type, defaults to 'billing_'.
Default value: 'billing_'
Return Return
(array)
Source Source
File: includes/class-wc-countries.php
public function get_address_fields( $country = '', $type = 'billing_' ) {
if ( ! $country ) {
$country = $this->get_base_country();
}
$fields = $this->get_default_address_fields();
$locale = $this->get_country_locale();
if ( isset( $locale[ $country ] ) ) {
$fields = wc_array_overlay( $fields, $locale[ $country ] );
}
// Prepend field keys.
$address_fields = array();
foreach ( $fields as $key => $value ) {
if ( 'state' === $key ) {
$value['country_field'] = $type . 'country';
$value['country'] = $country;
}
$address_fields[ $type . $key ] = $value;
}
// Add email and phone fields.
if ( 'billing_' === $type ) {
if ( 'hidden' !== get_option( 'woocommerce_checkout_phone_field', 'required' ) ) {
$address_fields['billing_phone'] = array(
'label' => __( 'Phone', 'woocommerce' ),
'required' => 'required' === get_option( 'woocommerce_checkout_phone_field', 'required' ),
'type' => 'tel',
'class' => array( 'form-row-wide' ),
'validate' => array( 'phone' ),
'autocomplete' => 'tel',
'priority' => 100,
);
}
$address_fields['billing_email'] = array(
'label' => __( 'Email address', 'woocommerce' ),
'required' => true,
'type' => 'email',
'class' => array( 'form-row-wide' ),
'validate' => array( 'email' ),
'autocomplete' => 'no' === get_option( 'woocommerce_registration_generate_username' ) ? 'email' : 'email username',
'priority' => 110,
);
}
/**
* Important note on this filter: Changes to address fields can and will be overridden by
* the woocommerce_default_address_fields. The locales/default locales apply on top based
* on country selection. If you want to change things like the required status of an
* address field, filter woocommerce_default_address_fields instead.
*/
$address_fields = apply_filters( 'woocommerce_' . $type . 'fields', $address_fields, $country );
// Sort each of the fields based on priority.
uasort( $address_fields, 'wc_checkout_fields_uasort_comparison' );
return $address_fields;
}