5

Does anybody know how to disable the Postcode validation on the checkout page in WooCommerce?

My country is set up as Switzerland, but I want also people from Austria and Germany allow to order.

So when I enter a German Postcode with 5 digits (in Switzerland there are only 4), the shop says it's an invalid postcode. (but in the settings I allowed every country).

Any idea how to fix that?

Knut Holm
  • 3,988
  • 4
  • 32
  • 54
marcelgo
  • 85
  • 1
  • 1
  • 5

4 Answers4

1

Adding this code to the functions.php file should work:

function custom_override_default_address_fields( $address_fields ) 
{
    unset( $address_fields['postcode'] );
    return $address_fields;
}

EDIT:

// Hook into the checkout fields (shipping & billing)
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Hook into the default fields
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );



function custom_override_checkout_fields( $fields ) 
{
    unset( $fields['billing']['billing_postcode'] );
    unset( $fields['shipping']['shipping_postcode'] );

    return $fields;
}

function custom_override_default_address_fields( $address_fields ) 
{
    unset( $address_fields['postcode'] );

    return $address_fields;
}

ANOTHER EDIT:

add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );

function custom_override_default_address_fields( $address_fields ) 
{
    $address_fields['postcode']['required'] = false;
    return $address_fields;
}
Howli
  • 12,291
  • 19
  • 47
  • 72
  • I added the code to the functions.php but unfortunately it still doesn't work :( – marcelgo Apr 27 '14 at 16:31
  • Nevermind :) But now I don't see any postcode field at all... It should be actually visible but I just want to be able to type in any kind of number. Sorry if my post was not clear. – marcelgo Apr 27 '14 at 17:08
  • Still saying the postcode is not valid :( Even if the field is not a mandatory field now... Maybe you have another idea ??? – marcelgo Apr 27 '14 at 19:24
  • That should do it. Maybe you have some plugin that is effecting that? – Howli Apr 27 '14 at 19:29
  • The only Plugin I use for WooCommerce is "Min/Max Quantities" which I already deactivated. Unfortunately, still the same issue. I guess there is a kind of pattern matching defined for the postcode somewhere... Guess I have to keep trying. But thank you very much for your help so far. – marcelgo Apr 28 '14 at 20:14
  • 2
    The above answer removes the postcode, whereas op wants to disable the validation only. I've been looking for an answer to the same question. – Mr.Vibe Jul 18 '14 at 08:54
1

So I didn't actually find a simple code solution for this one but I noticed that if I set

WooCommerce > Preferences > General > Geolocate address 

it will work (if settings are set to "Sell to all countries", in my case)

Robert
  • 5,278
  • 43
  • 65
  • 115
1

This code only removes validation of address fields in my-account page, what you need:

add_filter( 'woocommerce_default_address_fields',
   'custom_override_default_address_fields' );

function custom_override_default_address_fields($address_fields)
{
    $address_fields['postcode']['validate'] = false;
    return $address_fields;
}

for billing and shipping:

 add_filter( 'woocommerce_checkout_fields' , 'remove_postcode_validation', 99 );

function remove_postcode_validation( $fields ) {

unset($fields['billing']['billing_postcode']['validate']);
unset($fields['shipping']['shipping_postcode']['validate']);

return $fields;
}

Also i think with removing "validate-required" class in wc-template-function.php, this feature will be deactivated (no test).

Sorry for bad English and hope this solutions solve your problem.

nima
  • 97
  • 1
  • 2
  • 10
1

The previous answers don't seem to address the question! The postcode is still a required field, it's matter of whether other postcodes can be allowed that WooCommerce is saying is wrong.

For a checkout I'm building I've used this filter to allow any postcode to be considered valid regardless of country/postcode given.

add_filter( 'woocommerce_validate_postcode' , 'mycode_override_postcode_check', 99, 3 );
function mycode_override_postcode_check( $valid, $postcode, $country ) {
    return true;
}

You can change that return true; for more complex code logic that reviews the postcode and country, this can help if your version of WooCommerce doesn't cover new postcode/zip code rules and you need them to be accepted. Source: https://github.com/EloxZ/wordpresscrm/blob/main/wp-content/plugins/woocommerce/includes/class-wc-validation.php#L123

Greg Robson
  • 482
  • 4
  • 8