How to make the Checkout fields Required or optional with WooCommerce

woo develop

For various reasons, sometimes it is not possible to easily make all or some of the checkout fields required or optional in WooCommerce.

Either because the theme blocks this customization, or because some plugin has interfered with this default feature of WooCommerce, the reality is that there are times when you cannot make any or all of the fields required or optional when finalizing your customer’s purchase.

woocommerce checkout page options

The solution is to force these fields to be required or optional by means of a hook with the maximum level of priority, in case the reason is a plugin that already uses it previously.

The code is like this:

/** Change obligatory-optional status on billing fields * true = required * false = optional */ add_filter('woocommerce_billing_fields', 'force_billing_fields', 1000, 1); function force_billing_fields($fields) { $fields['billing_first_name']['required'] = false; //First Name $fields['billing_last_name']['required'] = false; //Last Name $fields['billing_email']['required'] = false; //Email $fields['billing_phone']['required'] = false; //Phone number return $fields; } /** Other billing fields * true = required * false = optional */ add_filter( 'woocommerce_default_address_fields', 'customize_extra_fields', 1000, 1 ); function customize_extra_fields( $address_fields ) { $address_fields['company']['required'] = false; //Company name $address_fields['address_1']['required'] = false; //Address $address_fields['country']['required'] = false; //Country $address_fields['city']['required'] = false; //City $address_fields['state']['required'] = false; //State $address_fields['postcode']['required'] = false; //Postcode return $address_fields; }
Code language: PHP (php)

In the example code I have chosen to make all the fields optional (false) but you can easily change to true simply by changing the one you want to make required.

You must add this code at the end of the active theme (child if possible) functions.php file or to your customizations plugin.

Read this post in Spanish: Cómo hacer obligatorios u opcionales los campos al finalizar compra en WooCommerce

Click to rate this post!
[Total: 0 Average: 0]

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top Skip to content