Home  >  Q&A  >  body text

Simplify WooCommerce checkout process: remove state/province field, making it "non-required" for all countries

<p>I found a way on GitHub to remove the state/province field 'state' in woocommerce's checkout page. https://gist.github.com/jeherve/a07ccf469025d722ad7016f6953146fd (Thanks Jeremy Herve!)</p> <pre class="brush:php;toolbar:false;">function jeherve_remove_state_field( $fields ) { unset( $fields['state'] ); return $fields; } add_filter( 'woocommerce_default_address_fields', 'jeherve_remove_state_field' );</pre> <p>My question is: Is it safe to remove the state/province field, or is it already set in woocommerce that certain countries require that field? </p> <p>Do I need to first set the state/province field for each country to 'optional'? </p> <p>I'm concerned that by removing the state/province field, the checkout page won't work properly for certain countries because they require that field. </p> <p>I don't need to set the state/province field to $address_fields['state'][required]=false like I do with the zip code field, right? </p> <pre class="brush:php;toolbar:false;">add_filter( 'woocommerce_default_address_fields' , 'override_postcode_validation' ); function override_postcode_validation( $address_fields ) { $address_fields['postcode']['required'] = false; return $address_fields; }</pre> <p>Thank you very much for your help. Thanks. </p>
P粉311617763P粉311617763393 days ago475

reply all(1)I'll reply

  • P粉392861047

    P粉3928610472023-08-26 13:32:55

    You can use the same code to remove the state/province field from the checkout form.

    function wc_remove_state_field($fields) {
    
        unset($fields['state']);
        return $fields;
    }
    
    add_filter('woocommerce_default_address_fields', 'wc_remove_state_field');

    This will not cause any problems with the checkout process. You don't need to set this for all countries.

    1 - Postal code still needs to be filled in to continue.

    2 - Payment gateways that require a state/province field cannot be used for checkout.

    reply
    0
  • Cancelreply