0
votes

I have already followed the method to make the checkout fields optional with the codes below. However, I found that the alert to fill in address field jumped out when the non-login user submit the order.

add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
function custom_override_default_address_fields($address_fields) {

    $address_fields['first_name']['required'] = false;
    $address_fields['last_name']['required'] = false;
    $address_fields['address_1']['required'] = false;
    $address_fields['address_1']['placeholder'] = '';
    $address_fields['address_2']['required'] = false;
    $address_fields['address_2']['placeholder'] = '';
    $address_fields['postcode']['required'] = false;
    $address_fields['city']['required'] = false;

return $address_fields;
}

By the way, I also tried to make the billing and shipping fields optional separately as the method in the link WooCommerce: Disabling checkout fields with a filter hook.

2
Your code just works perfectly for logged or non logged in users…LoicTheAztec
I think so. But the alert to fill in address field for non logged in users and the alert to fill in postcode for logged in users.LEELIN

2 Answers

1
votes

Depending on which checkout fields you would like to make optional (or required), there are two filters that you will need to hook into. The first filter is woocommerce_default_address_fields and is for the address fields, the second is woocommerce_billing_fields and is for the billing_phone and billing_email fields.

add_filter( 'woocommerce_default_address_fields', 'adjust_requirement_of_checkout_address_fields' );
function adjust_requirement_of_checkout_address_fields( $fields ) {
    $fields['first_name']['required']   = false;
    $fields['last_name']['required']    = false;
    $fields['company']['required']      = false;
    $fields['country']['required']      = false;
    $fields['address_1']['required']    = false;
    $fields['address_2']['required']    = false;
    $fields['city']['required']         = false;
    $fields['state']['required']        = false;
    $fields['postcode']['required']     = false;

    return $fields;
}

add_filter( 'woocommerce_billing_fields', 'adjust_requirement_of_checkout_contact_fields');
function adjust_requirement_of_checkout_contact_fields( $fields ) {
    $fields['billing_phone']['required']    = false;
    $fields['billing_email']['required']    = false;

    return $fields;
}
-1
votes

go to this thread Make checkout fields required in Woocommerce checkout and change true to false for the fields you don't need to be required ex : change $address_fields['postcode']['required'] = true; to $address_fields['postcode']['required'] = false;