简体   繁体   中英

Editing WooCommerce checkout page billing address fields

I want to edit the billing address of WooCommerce checkout page. I want to edit the billing State of my checkout page. I tried editing it by first doing this in my child theme.

Then I have tried editing class-wc-checkout.php file:

// Billing address
$billing_address = array();
    if ( $this->checkout_fields['billing'] ) {
        foreach ( array_keys( $this->checkout_fields['billing'] ) as $field ) {
            $field_name = str_replace( 'billing_', '', $field );
            $billing_address[ $field_name ] = $this->get_posted_address_data( $field_name );
        }
    }

Without success. How can I do this?

Thanks.

Important advice: Never touch WooCommerce plugin core files avoiding:

  • Important errors
  • Losing the changes you have done when plugin is updated

To customize WooCommerce you can:


To edit / create / remove / reorder checkout fields we can use this 2 filter hooks:

  • 'woocommerce_checkout_fields'

or in specific cases you need to use the

  • 'woocommerce_default_address_fields' (applied to all billing & shipping default fields below)

Here the list of billing and shipping default fields:

country
first_name
last_name
company
address_1
address_2
city
state
postcode

For example, to make the 'billing_state' field required:

add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
function custom_override_default_address_fields( $address_fields ) {

    // we are changing here billing_state field to required
    $address_fields['billing']['billing_state']['required'] = true;

    return $address_fields;
}

Each field contains an array of properties that you can edit:

type – type of field (text, textarea, password, select)
label – label for the input field
placeholder – placeholder for the input
class – class for the input
required – true or false, whether or not the field is require
clear – true or false, applies a clear fix to the field/label
label_class – class for the label element
options – for select boxes, array of options (key => value pairs)

Checkout fields are divided into 4 groups:

  • shipping fields
  • billing fields
  • account fields
  • order fields (notes, comments)

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM