Artem Kopytko
Artem Kopytko

Reputation: 126

Make checkout addresses fields not required in WooCommerce

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?

Upvotes: 4

Views: 25689

Answers (3)

LoicTheAztec
LoicTheAztec

Reputation: 253867

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.

Upvotes: 25

Mookie
Mookie

Reputation: 41

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/

Upvotes: 3

Artem Kopytko
Artem Kopytko

Reputation: 126

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

Upvotes: 1

Related Questions