Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to make fields required or optional on the WooCommerce Checkout page

On the WooCommerce Checkout page, we see that there are some fields which are absolutely required while some can be left vacant.

Depending on your use case however, you may sometimes need your customers to treat an optional field as mandatory, or a mandatory field as optional. This post explains how you can modify the fields & make fields required or optional on the WooCommerce Checkout page.

Example 1: Making the Phone field optional

In the latest versions of WooCommerce, the Phone field (under Billing Details) is a required field by default when you are on the Checkout page.

Make fields required or optional on the WooCommerce Checkout page - Make Phone number field optional

What if you want to make this field optional for cases where it isn’t necessary and where customers too do not wish to disclose this information?

The code snippet below enables you to do the same:

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

The first line here declares a function named as “ts_unrequire_wc_phone_field”. You can name your function anything you want.

‘billing_phone’ refers to the name of the Phone field that we want to make optional.

Inside the function,  $fields[‘billing_phone’][‘required’] is assigned a “false” value implying that the field is not required or is optional.

Adding the lines above to your child theme‘s function.php file will make the Phone field optional on the Checkout page, as depicted in the image below.

Make fields required or optional on the WooCommerce Checkout page - Make Phone number field optional

There you go! The Phone field is now optional.

flexi bogo cta banner image


This to the shop owners who are running or planning to run BOGO offers on their WooCommerce store…

BOGO deals are great for increasing your sales, but have you thought about which offers are bringing you more revenue and which offers are not performing that great?

Don’t just set a BOGO deal, track the revenue generated by your deals in real-time with the Flexi BOGO for WooCommerce plugin.

Example 2: Making the Company Name field required

On the Checkout page, we see that the company name field is optional by default:

Make fields required or optional on the WooCommerce Checkout page - Make Company field required

There are many cases where you may want the Company Name to be a compulsory field. For example, sale of tickets to a corporate conference or seminar, or the purchase of a corporate license for a product. In this case, the same code snippet may be used with a few modifications to make the Company Name field a required field.

add_filter( 'woocommerce_billing_fields', 'ts_require_wc_company_field');
function ts_require_wc_company_field( $fields ) {
$fields['billing_company']['required'] = true;
return $fields;
}

Here, assigning a value of “true” to $fields[‘billing_company’][‘required’] does the job.

The result is that the company name field becomes a required field:

Make fields required or optional on the WooCommerce Checkout page - Make Company field required

Below are the HTML names of all the fields on the Checkout page. The names are indicative of which field they correspond to. You can refer to the snippets above to change any of these fields into an optional or a required field, depending on the use case.

Billing Fields

  • billing_first_name
  • billing_last_name
  • billing_company
  • billing_country
  • billing_address_1
  • billing_address_2
  • billing_city
  • billing_state
  • billing_postcode
  • billing_phone
  • billing_email

Shipping Fields

  • shipping_first_name
  • shipping_last_name
  • shipping_company
  • shipping_country
  • shipping_address_1
  • shipping_address_2
  • shipping_city
  • shipping_state
  • shipping_postcode

Order Fields

  • order_comments

With just a few lines of code, you can change any field on your Checkout page into an optional or a mandatory field.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
11 Comments
Newest
Oldest
Inline Feedbacks
View all comments
Laurent
1 year ago

Hi, how can you change when you just want to select specific country selected to change it to be mandatory. Like if selected country and state is not mandatory.
Thanks

Chethiya Kusal
3 years ago

Really helpful

Carolina
3 years ago

Where do I add this code to? I’m a total beginner.

Anonymous
2 years ago
Reply to  Carolina

the functions.php file, if you don’t have one, make it inside the root of your theme directory

4 years ago

Hi, thank you very much it’s working and I don’t have to use another plugins to do so …

5 years ago

Hi,

Really helpful article. I added a couple extensions to WordPress and suddenly all my checkout fields are showing as option.

Is it necessary to add the snipets through a child theme though? Can it be done through one of the other means you mentioned: Using a Plugin, oe Creating a Custom Plugin? I’m out of my depth creating child themes.

Thanks

11
0
Would love your thoughts, please comment.x
()
x