Solved: Un-require WooCommerce order submission fields

You need to make the entry of some fields in WooCommerce registration non-required or optional. Maybe you even want to hide some fields from displaying at all to make things clearer and simplier. I know I did, I didn't need all the fields available so I hid them. What happened when I tried to submit an order? Turned out some fields I had hidden via css were required fields.

How to make WooCommerce registration fields optional (non required)?


If you are already into WordPress development then you have an idea what a functions.php file is. Just take this piece of code and paste it in your functions.php to change some fields (in this code they are billing_city and billing_postcode) from required to optional:

 

/*required: false to some WooCommerce fields */

add_filter( 'woocommerce_billing_fields', 'jb_filter_fields', 10, 1 );

function jb_filter_fields( $address_fields ) {
$address_fields['billing_city']['required'] = false;
$address_fields['billing_postcode']['required'] = false;
return $address_fields;
}

 


To make this effective on some other fields, just add lines to the code similar to what you see for billing_city and billing_postcode.

The full list of fields that are being passed and can be altered this way:

Billing
billing_first_name
billing_last_name
billing_company
billing_address_1
billing_address_2
billing_city
billing_postcode
billing_country
billing_state
billing_email
billing_phone
Shipping
shipping_first_name
shipping_last_name
shipping_company
shipping_address_1
shipping_address_2
shipping_city
shipping_postcode
shipping_country
shipping_state
Account
account_username
account_password
account_password-2
Order
order_comments

 

So you can add any of those to be non-required when submitting orders.

Ps, all the fields have an array of properties and "required" is just one of the properties.

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)

About me

Iinstructor of robotics (LEGO Mindstorms), computer teacher, IT support, web dev, currently studying cyber-physical systems engineering.

If you found a solution here, would you consider donating $25,000 for my next project?

Just kidding, 1$ would do fine.

Hope you found what you needed. Thanks for stepping by.