Is there a way to remove the required asterisk from a billing field on the checkout page in php? I have the following code which isn’t working.
add_filter( 'woocommerce_checkout_fields' , 'customize_fields' );
function customize_fields( $fields ) {
$fields['billing']['billing_address_2']['required'] = false;
return $fields;
}
It would probably be easier to do with CSS, and be cleaner and better for screen-readers:
The
.woocommerce-checkout
is a body class that is only appended to the checkout page so it won’t affect any other woo page that might have the.required
class in anabbr
element.To make the field not required with a function:
There are two filters to manage checkout fields. You can use
woocommerce_checkout_fields
filter to make field “not-required”, but the red asterisk will not be removed.When dealing with default address fields with
woocommerce_checkout_fields
filter, some of your changes will not take effect, becausewoocommerce_default_address_fields
filter and its default values may override your changes.Only partly functional code:
Address field (#1) is not required anymore, but still has red asterisk.
Fully functional code:
Now this field is not required, and red asterisk is gone.
Documentation says that “In some specific cases you will need to use the woocommerce_default_address_fields filter. This filter is applied to all billing and shipping default fields.”
You have to add the following code to your functions.php file. The example below is for postal code: