WC_Checkout::initialize_checkout_fields
Initialize the checkout fields.
Method of the class: WC_Checkout{}
Hooks from the method
Returns
null. Nothing (null).
Usage
// protected - for code of main (parent) or child class $result = $this->initialize_checkout_fields();
WC_Checkout::initialize_checkout_fields() WC Checkout::initialize checkout fields code WC 10.5.0
protected function initialize_checkout_fields() {
// Fields are based on billing/shipping country. Grab those values but ensure they are valid for the store before using.
$billing_country = $this->get_value( 'billing_country' );
$billing_country = empty( $billing_country ) ? WC()->countries->get_base_country() : $billing_country;
$allowed_countries = WC()->countries->get_allowed_countries();
if ( ! array_key_exists( $billing_country, $allowed_countries ) ) {
$billing_country = current( array_keys( $allowed_countries ) );
}
$shipping_country = $this->get_value( 'shipping_country' );
$shipping_country = empty( $shipping_country ) ? WC()->countries->get_base_country() : $shipping_country;
$allowed_countries = WC()->countries->get_shipping_countries();
if ( ! array_key_exists( $shipping_country, $allowed_countries ) ) {
$shipping_country = current( array_keys( $allowed_countries ) );
}
$this->fields = array(
'billing' => WC()->countries->get_address_fields(
$billing_country,
'billing_'
),
'shipping' => WC()->countries->get_address_fields(
$shipping_country,
'shipping_'
),
'account' => array(),
'order' => array(
'order_comments' => array(
'type' => 'textarea',
'class' => array( 'notes' ),
'label' => __( 'Order notes', 'woocommerce' ),
'placeholder' => esc_attr__(
'Notes about your order, e.g. special notes for delivery.',
'woocommerce'
),
),
),
);
if ( 'no' === get_option( 'woocommerce_registration_generate_username' ) ) {
$this->fields['account']['account_username'] = array(
'type' => 'text',
'label' => __( 'Account username', 'woocommerce' ),
'required' => true,
'placeholder' => esc_attr__( 'Username', 'woocommerce' ),
'autocomplete' => 'username',
);
}
if ( 'no' === get_option( 'woocommerce_registration_generate_password' ) ) {
$this->fields['account']['account_password'] = array(
'type' => 'password',
'label' => __( 'Create account password', 'woocommerce' ),
'required' => true,
'placeholder' => esc_attr__( 'Password', 'woocommerce' ),
'autocomplete' => 'new-password',
);
}
/**
* Sets the fields used during checkout.
*
* @since 3.0.0 or earlier
*
* @param array[] $checkout_fields
*/
$this->fields = apply_filters( 'woocommerce_checkout_fields', $this->fields );
foreach ( $this->fields as $field_type => $fields ) {
// Sort each of the checkout field sections based on priority.
uasort( $this->fields[ $field_type ], 'wc_checkout_fields_uasort_comparison' );
// Add accessibility labels to fields that have placeholders.
foreach ( $fields as $single_field_type => $field ) {
if ( empty( $field['label'] ) && ! empty( $field['placeholder'] ) ) {
$this->fields[ $field_type ][ $single_field_type ]['label'] = $field['placeholder'];
$this->fields[ $field_type ][ $single_field_type ]['label_class'] = array( 'screen-reader-text' );
}
}
}
}