skip to Main Content

“state” field in woocommerce checkout page is not appearing when choosing certain countries like germany, so when the customer clicks place order, and error appears that he needs to put the state, however its not possible to put the state since there is no field for it.

1- if he chooses countries like United Arab Emirates, USA, Canada, etc…, a state field appears and the customer can fill it normally and the payment goes through with no problems.

2- if Germany, or Afghanistan or some different countries are chosen, the “state/county” field disappears, and when the customer clicks “place order” and error appears: “shipping_state post variable not found”.

i’m using paytabs as a payment gateway.

I’m not sure what code to edit, but woocommerce shows “state” fields dynamically based on chosen country.

error appears that the customer needs to put the state, however its not possible to put the state since there is no field for it.

2

Answers


  1. As default woocommerce does not show state field for some countries you can see those countries in class-wc-countries.php

    the below code will allow state field for Afghanistan please test and edit to your suiting

    function so_57838961_filter_woocommerce_states( $states ) { 
        unset( $states['AF'] );
        var_dump( $states ) ;
        return $states;
    };
    add_filter( 'woocommerce_states', 'so_57838961_filter_woocommerce_states', 10, 1 );
    
    function so_57838961_filter_woocommerce_get_country_locale( $locale ) { 
        $locale['AF']['state']['required'] = true;
        return $locale; 
    };
    add_filter( 'woocommerce_get_country_locale', 'so_57838961_filter_woocommerce_get_country_locale', 10, 1 ); 
    
    Login or Signup to reply.
  2. The error you are experiencing is the line with the "var_dump", which is outputting all the states information to the screen. Just remove that line and it works perfectly, I just did this on my website with Austria "AT".

    For more information, just google "php var_dump" and it tells you what that function does.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search