WC_Countries::get_address_fields
Apply locale and get address fields.
Method of the class: WC_Countries{}
Hooks from the method
Returns
Array.
Usage
$WC_Countries = new WC_Countries(); $WC_Countries->get_address_fields( $country, $type );
- $country(mixed)
- Country.
Default:'' - $type(string)
- Address type.
Default:'billing_'
WC_Countries::get_address_fields() WC Countries::get address fields code WC 10.7.0
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();
// Convert type prefix (e.g., 'billing_' or 'shipping_') to address type for autocomplete (e.g., 'billing' or 'shipping').
$address_type = rtrim( $type, '_' );
foreach ( $fields as $key => $value ) {
if ( 'state' === $key ) {
$value['country_field'] = $type . 'country';
$value['country'] = $country;
}
// Prefix autocomplete value with section and address type per HTML spec.
// Format: section-<name> [shipping|billing] <autofill-field>
// e.g., 'address-level1' becomes 'section-billing billing address-level1'.
if ( ! empty( $value['autocomplete'] ) ) {
$value['autocomplete'] = 'section-' . $address_type . ' ' . $address_type . ' ' . $value['autocomplete'];
}
$address_fields[ $type . $key ] = $value;
}
// Add email and phone fields.
if ( 'billing_' === $type ) {
if ( 'hidden' !== CartCheckoutUtils::get_phone_field_visibility() ) {
$address_fields['billing_phone'] = array(
'label' => __( 'Phone', 'woocommerce' ),
'required' => 'required' === CartCheckoutUtils::get_phone_field_visibility(),
'type' => 'tel',
'class' => array( 'form-row-wide' ),
'validate' => array( 'phone' ),
'autocomplete' => 'section-' . $address_type . ' ' . $address_type . ' 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' => 'section-' . $address_type . ' ' . $address_type . ' email',
'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;
}