4

In Woocommerce I am trying to make checkout adresses fields not required with the code below… But I've got this error "Please enter an address to continue", when submitting checkout form.

My code to set adresses checkout fields not required:

add_filter( 'woocommerce_billing_fields', 'wc_npr_filter_phone', 10, 1 
);
function wc_npr_filter_phone( $address_fields ) {
    $address_fields['billing_phone']['required'] = true;
    $address_fields['billing_country']['required'] = false;
    $address_fields['billing_last_name']['required'] = false;
    $address_fields['billing_city']['required'] = false;
    $address_fields['billing_postcode']['required'] = false;
    $address_fields['billing_email']['required'] = false;
    $address_fields['billing_state']['required'] = false;
    $address_fields['billing_address_1']['required'] = false;
    $address_fields['billing_address_2']['required'] = false;
    return $address_fields;

}

//make shipping fields not required in checkout
add_filter( 'woocommerce_shipping_fields', 
'wc_npr_filter_shipping_fields', 10, 1 );
function wc_npr_filter_shipping_fields( $address_fields ) {
    $address_fields['shipping_first_name']['required'] = false;
    $address_fields['shipping_last_name']['required'] = false;
    $address_fields['shipping_address_1']['required'] = false;
    $address_fields['shipping_address_2']['required'] = false;
    $address_fields['shipping_city']['required'] = false;
    $address_fields['shipping_country']['required'] = false;
    $address_fields['shipping_postcode']['required'] = false;
    $address_fields['shipping_state']['required'] = false;
    return $address_fields;
}

The HTML seems to be okay too:

HTML markup screenshot

How to make checkout addresses fields not required in WooCommerce?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Artem Kopytko
  • 128
  • 1
  • 1
  • 7

3 Answers3

24

You should need to use woocommerce_default_address_fields filter hook instead as explained here.

The replacement code:

// Billing and shipping addresses fields
add_filter( 'woocommerce_default_address_fields' , 'filter_default_address_fields', 20, 1 );
function filter_default_address_fields( $address_fields ) {
    // Only on checkout page
    if( ! is_checkout() ) return $address_fields;

    // All field keys in this array
    $key_fields = array('country','first_name','last_name','company','address_1','address_2','city','state','postcode');

    // Loop through each address fields (billing and shipping)
    foreach( $key_fields as $key_field )
        $address_fields[$key_field]['required'] = false;

    return $address_fields;
}

As billing email and phone are already required by default, if you want them to be not required, you should need this additional code:

// For billing email and phone - Make them not required
add_filter( 'woocommerce_billing_fields', 'filter_billing_fields', 20, 1 );
function filter_billing_fields( $billing_fields ) {
    // Only on checkout page
    if( ! is_checkout() ) return $billing_fields;

    $billing_fields['billing_phone']['required'] = false;
    $billing_fields['billing_email']['required'] = false;
    return $billing_fields;
}

All code goes in functions.php file of your active child theme (or active theme). Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks for fast response. Provided code not helped and I'm still getting that error. What can be a reason of a such behavior? – Artem Kopytko May 01 '18 at 16:32
  • @ArtemKopytko Something like your theme or some plugin is may be making customizations on checkout fields… My answer code is something classic and really works. – LoicTheAztec May 01 '18 at 16:43
  • @LoicTheAztec I've set the country field to required but the form submits even if a country is not selected & also no error message is shown for country required. – DavidG Sep 24 '20 at 07:23
  • @LoicTheAztec can we also make "billing_name" not required ? – user3304007 Oct 13 '20 at 12:14
1

The only thing helped me is to add a field for country and make it invisible (because I don't need it)

Artem Kopytko
  • 128
  • 1
  • 1
  • 7
1
add_filter( 'woocommerce_checkout_fields', 'your_require_wc_phone_field');
function your_require_wc_phone_field( $fields ) {
    $fields['billing']['billing_phone']['required'] = false;
    return $fields;
}

This method uses 'woocommerce_checkout_fields' which when testing on my own sites was the only way I could change the billing_phone field to required for my problem.

https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/

Mookie
  • 21
  • 3
  • 1
    Can you add some explanation as to why your answer is preferred over the accepted answer? Your answer looks pretty similar to the second part of the accepted one. – Simply Ged Jun 12 '19 at 23:22