Here’s an Ask SkyVerge question from Brett:

It looks like all of the WooCommerce checkout fields are added in the billing form template but I’m not sure where they’re coming from. Is there an easy way to change which of these fields are required? I don’t need the phone, but I do need the company field instead and I’m not seeing where the fields are.


The WooCommerce checkout fields are generated outside of the checkout template, so we have to do a bit of digging to find them. At the top of the billing form template, we notice that we’re using the $checkout global: @global WC_Checkout $checkout

This tells us we need to look at the WC_Checkout class to find where these fields are being generated instead, and we’ll go down a tiny rabbit hole 🙂 .

The checkout class defines the checkout fields for billing and shipping, and it pulls them from WC()->countries->get_address_fields. This means we have to dig down one more level, and go to /includes/class-wc-countries.php. As these fields may change by country or state / province, they’re generated here when we check the country, and then the checkout template outputs the right fields in the right order for the selected country.

The first thing I’m going to do is find the “company” field. I do a bit of searching, and find that it’s generated by get_default_address_fields, and can be modified by the woocommerce_default_address_fields filter, which passes in all of these fields.

I’ll first add a tiny snippet to my child theme’s functions.php or custom plugin to adjust this:

function sv_require_wc_company_field( $fields ) {
    $fields['company']['required'] = true;
    return $fields;
}
add_filter( 'woocommerce_default_address_fields', 'sv_require_wc_company_field' );

Voilà, our company field is now required. Now let’s find where the phone field is generated.

We can keep searching through the countries class, and find that get_address_fields will generate the email and phone fields, as these are specific to billing addresses. A filter is generated here that includes the form type so we can change both billing and shipping fields. We replace “type” with “billing_” to get a woocommerce_billing_fields filter.

Now we can use this filter to change “required” from true to false for the billing phone field:

function sv_unrequire_wc_phone_field( $fields ) {
    $fields['billing_phone']['required'] = false;
    return $fields;
}
add_filter( 'woocommerce_billing_fields', 'sv_unrequire_wc_phone_field' );

When these two snippets are added, the “Company” field will be required, and the “Phone” field is no longer required.

Updated WooCommerce Checkout Fields

New Required Fields

As a note to take things further, the woocommerce_billing_fields does pass in the country as an optional argument, so you could use this to only conditionally require the phone fields for certain countries. Our guide to filters can help out there.

Published by Beka Rice

Beka manages WooCommerce product direction for SkyVerge, such as plugin roadmaps, documentation, and sales copy. She typically gets stuck with boring administrative tasks, but likes to write so she has an excuse to spend more time jamming out to anything from The Clash to Lady Gaga.

11 Comments

  1. […] a guide on how change which fields are required in the WooCommerce checkout from […]

  2. Hi, just wondering if you can make the company field required for a specific product only? Say product id 1234 and not required for every other product.

    Also how to you rename the company name label to something else?

    Thanks

    • just wondering if you can make the company field required for a specific product only? Say product id 1234 and not required for every other product.

      This is possible, but requires a lot more code to check what’s in the cart first.

      Also how to you rename the company name label to something else?

      I’d recommend checking out the Say What plugin, which can let you replace “Company Name:” with your own label.

  3. Hi Beka,
    I found your article very interesting. That was just what I was looking for!
    But I was wondering if you know how to add a question mark beside a form field (email field) which displays some text when the mouse is over it (such as this one: http://dlcandtraining.co.uk/core/wp-content/uploads/example55.jpg).
    I know how to do that, but I don’t know where I have to add this code.
    Thanks
    Paloma

  4. GitHub link is not more valid, thanks for this useful tutorial.

  5. This was a great start point but until I realised that you can have ‘billing_postcode’ and ‘shipping_postcode’ but if you have just ‘postcode’ then that’s the one to use.

  6. Thanks a lot for the hint ! 🙂

  7. If you want to remove validation on address fields, see this. Took me about 3 hours to figure it out!

    https://www.sitekickr.com/snippets/wordpress/remove-woocommerce-checkout-billing-field-validation

    • Phil LaNasa
      Can you explain me in detail. After removing the phone field, I see an error ”
      This field may not be blank.”

  8. Hello. I use woocommerce checkout manager plugin and I was struggling with “billing phone” input mask. You see, I need to restrict the field so that a user can only write numbers for phone and I also want to use a format like (5_ _ , _ _ _ _ _ _ _)

    I would really appreciate if you could shad some light on this issue. Thank you!

Comments are closed.

Error: Please enter a valid email address

Error: Invalid email

Error: Please enter your first name

Error: Please enter your last name

Error: Please enter a username

Error: Please enter a password

Error: Please confirm your password

Error: Password and password confirmation do not match