Make checkout addresses fields not required in WooCommerce

佐手、 提交于 2020-01-03 06:01:29

问题


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:

How to make checkout addresses fields not required in WooCommerce?


回答1:


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 function.php file of your active child theme (or active theme). Tested and works.




回答2:


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/




回答3:


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



来源:https://stackoverflow.com/questions/50107409/make-checkout-addresses-fields-not-required-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!