Customise WooCommerce checkout fields with PHP to add, remove, and reorder

WooCommerce checkout fields: remove, make optional, reorder

The default WooCommerce checkout asks for a company, a second address line, a state and a phone number. A store that sells downloads or delivers only in one city does not need half of it, and every extra field costs orders. All checkout fields go through one filter, so removing, relabelling and reordering them is a few lines of PHP.

Problem: The checkout should not ask for the company and the second address line, the phone should be optional, and the email should come right after the name.

Solution: Change the billing-only fields (email, phone) in the woocommerce_checkout_fields filter, and the address fields (company, address, city, state, postcode) in woocommerce_default_address_fields, because the address fields are rearranged per country afterwards and would ignore changes made in the first filter.

How to add a new field and save it to the order: add a custom field to the WooCommerce checkout.

Address fields

add_filter( 'woocommerce_default_address_fields', 'ha_address_fields' );

function ha_address_fields( $fields ) {
    unset( $fields['company'], $fields['address_2'] );

    $fields['postcode']['required'] = false;
    $fields['city']['priority']     = 45; // before the street address (50)

    return $fields;
}

These are the fields without prefix; WooCommerce copies them to billing_* and shipping_*. Then it applies the country rules (woocommerce_get_country_locale): for some countries the state is required or hidden, the postcode label changes, and the order of fields can change. That is why priorities of address fields set in woocommerce_checkout_fields often have no visible effect.

Billing-only fields

add_filter( 'woocommerce_checkout_fields', 'ha_checkout_fields' );

function ha_checkout_fields( $fields ) {
    $fields['billing']['billing_phone']['required'] = false;
    $fields['billing']['billing_email']['priority'] = 25; // after first name (10) and last name (20)

    $fields['order']['order_comments']['placeholder'] = __( 'Gate code, delivery time, ...', 'textdomain' );

    return $fields;
}

The default priorities are 10, 20, 30… (first name, last name, company, country, address 1, address 2, city, state, postcode, phone, email), so a field goes between two others with a priority between theirs.

A required state for one country only

add_filter( 'woocommerce_get_country_locale', function ( $locale ) {
    $locale['DE']['state']['required'] = false;
    $locale['DE']['state']['hidden']   = true;
    return $locale;
} );

Keep the account page in sync

Removed address fields also disappear from “My account, Addresses”, because the same default fields are used there. If the phone is optional at checkout but required in the account form, set it in woocommerce_billing_fields, which both use.

NOTE: A field you remove is no longer validated or saved, but payment gateways and shipping plugins may need it (some require the phone or the postcode). Test a real order with each payment method after the change.

Update (2023): These filters change the classic [woocommerce_checkout] shortcode. The Checkout block, the default for new stores since WooCommerce 8.3, has its own settings for the company, address line 2 and phone fields in the editor and does not use these PHP filters.