1

I am having post code based minimum order amount for orders for my Woocommerce shop. Also I am having postcode based custom error messages to appear once the user type postcode like how much he needs more to checkout. So preventing the user to type invalid postcodes(which means the postcodes that we do not deliver), i am displaying drop down to pick his valid postcode in Checkout page.

Based on this thread Change postcode shipping field to a dropdown in Woocommerce, i could turn the postcode field to dropdown with the options.

But i am getting the error that the "Postcode is not valid" by Woocommerce when i select any option from the drop down. Also my custom messages also not displaying.

Below is the code I m using from the above thread.

add_filter( 'woocommerce_default_address_fields' , 'customize_postcode_fields' );
function customize_postcode_fields( $adresses_fields ) {

$adresses_fields['postcode']['type'] = 'select';
$adresses_fields['postcode']['options'] = array(
'' => __('Select your postcode', 'woocommerce'),
'option_1' => '000001',
'option_2' => '000002',
'option_3' => '000003'
);

return $adresses_fields;

}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Tom
  • 87
  • 1
  • 9

1 Answers1

1

Try to use the following instead:

add_filter( 'woocommerce_default_address_fields' , 'customize_postcode_fields' );
function customize_postcode_fields( $adresses_fields ) {
    $adresses_fields['postcode']['type'] = 'select';
    $adresses_fields['postcode']['input_class'] = array('state_select');
    $adresses_fields['postcode']['options'] = array(
        '' => __('Select your postcode', 'woocommerce'),
        '000001' => '000001',
        '000002' => '000002',
        '000003' => '000003'
    );

    return $adresses_fields;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works on last WooCommerce version 4.5.1 under Storefront theme.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hi, thanks for the answer, but still Woocommerce is saying postcode is not valid when i select one of the option from dropdown.... – Tom Sep 12 '20 at 15:09
  • Should i need to change anyting in Woocommerce core files? – Tom Sep 12 '20 at 15:15
  • @Tom Is there any customizations that you have done related to checkout fields? If yes you should provide all related code in your question, with more explanations and details… Now I need to make some testing on my side and I come back to you. – LoicTheAztec Sep 12 '20 at 15:47
  • @Tom Just tested that on last Wordpress/WooCommerce versions under Storefront theme and it works perfectly without throwing any error when placing an order. So the problem comes from your other customizations, or a third party plugin or maybe your theme. – LoicTheAztec Sep 12 '20 at 16:05
  • Particularly for "woocommerce_default_address_fields" hook, i did not do any customizations. I am just displaying minimum order amount error messages according to customer postcode – Tom Sep 12 '20 at 17:38
  • Those error messages also not displaying when i select a postcode from the dropdown – Tom Sep 12 '20 at 17:40
  • Also I am not using third party plugin such as Checkout Manger or Checkout Editor even.... – Tom Sep 12 '20 at 18:06
  • Sorry for the few comments, the error message i am getting is "Billing Postcode / ZIP is not a valid postcode / ZIP.". At the same time I am using this "WC()->customer->get_shipping_postcode()" to get the customer postcode afer typed to display error message. Also when i tried to implement your code with "woocommerce_shippings_fields" hook, i did not get the drop down even. So, is this right? – Tom Sep 12 '20 at 18:56