2
votes

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;
}
3
Did you read this docs.woothemes.com/document/…? If your code doesn't work may you have do something else wrong. My suggestion is to use default template of woo to display checkout and see if the problem still exists.Thanassis

3 Answers

2
votes

It would probably be easier to do with CSS, and be cleaner and better for screen-readers:

 .woocommerce-checkout abbr.required {
       display: none;
     }

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 an abbr element.

To make the field not required with a function:

// Hook in
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );

// Our hooked in function - $address_fields is passed via the filter!
function custom_override_default_address_fields( $address_fields ) {
     $address_fields['address_2']['required'] = false;

     return $address_fields;
}
2
votes

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, because woocommerce_default_address_fields filter and its default values may override your changes.

Only partly functional code:

function custom_override_checkout_fields( $fields ) {
    $fields['billing']['billing_address_1']['required'] = false ;
    return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

Address field (#1) is not required anymore, but still has red asterisk.

Fully functional code:

function custom_override_default_address_fields( $address_fields ) {
    $address_fields['address_1'][ 'required' ] = false;
    return $address_fields;
}
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );

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."

0
votes

You have to add the following code to your functions.php file. The example below is for postal code:

 /* Seteaza campul Cod postal ne-obligatoriu */
    add_filter( 'woocommerce_billing_fields', 'wc_npr_filter_postcode', 10, 1 );
function wc_npr_filter_postcode( $address_fields ) {
    $address_fields['billing_postcode']['required'] = false;
    return $address_fields;
}
/* End - Seteaza campul Cod postal ne-obligatoriu  */