Search the site:

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

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. (To know why we edit the functions.php of the child theme and not directly the functions.php file, read this post.)

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

There you go! The Phone field is now optional.

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.

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of

Subscribe Now