Add/Modify States for WooCommerce Checkout Form

Sharing is caring!

If you want to modify the default states list that was populated when you select the billing and shipping country field in WooCommerce checkout form, then you can get some ideas from this tutorial.

For example, you want to restrict certain states for shipping products in United States: Alabama, Kentucky and Tennessee.

To customize the list, you need to follow the steps below:

Step 1: Open functions.php file in your theme folder and add below code:

add_filter( 'woocommerce_states', 'custom_woocommerce_states' );

function custom_woocommerce_states( $states ) {
  $states['US'] = array(
    'AL' => 'Alabama',
    'KY' => 'Kentucky',
    'TN' => 'Tennessee'
  );

  return $states;
}


Step 2: Save the file and refresh the checkout page. That’s it.

To see more default country and state code, please refer to this file – “wp-content\plugins\woocommerce\classes\class-wc-countries.php” at your project folder.

Stay tuned!

Reference for Country Code:

'AF' => Afghanistan
'AX' => Åland Islands
'AL' => Albania
'DZ' => Algeria
'AS' => American Samoa
'AD' => Andorra
'AO' => Angola
'AI' => Anguilla
'AQ' => Antarctica
'AG' => Antigua and Barbuda
'AR' => Argentina
'AM' => Armenia
'AW' => Aruba
'AU' => Australia
'AT' => Austria
'AZ' => Azerbaijan
'BS' => Bahamas
'BH' => Bahrain
'BD' => Bangladesh
'BB' => Barbados
'BY' => Belarus
'BE' => Belgium
'BZ' => Belize
'BJ' => Benin
'BM' => Bermuda
'BT' => Bhutan
'BO' => Bolivia
'BA' => Bosnia and Herzegovina
'BW' => Botswana
'BR' => Brazil
'IO' => British Indian Ocean Territory
'VG' => British Virgin Islands
'BN' => Brunei
'BG' => Bulgaria
'BF' => Burkina Faso
'BI' => Burundi
'KH' => Cambodia
'CM' => Cameroon
'CA' => Canada
'CV' => Cape Verde
'KY' => Cayman Islands
'CF' => Central African Republic
'TD' => Chad
'CL' => Chile
'CN' => China
'CX' => Christmas Island
'CC' => Cocos (Keeling) Islands
'CO' => Colombia
'KM' => Comoros
'CG' => Congo (Brazzaville)
'CD' => Congo (Kinshasa)
'CK' => Cook Islands
'CR' => Costa Rica
'HR' => Croatia
'CU' => Cuba
'CY' => Cyprus
'CZ' => Czech Republic
'DK' => Denmark
'DJ' => Djibouti
'DM' => Dominica
'DO' => Dominican Republic
'EC' => Ecuador
'EG' => Egypt
'SV' => El Salvador
'GQ' => Equatorial Guinea
'ER' => Eritrea
'EE' => Estonia
'ET' => Ethiopia
'FK' => Falkland Islands
'FO' => Faroe Islands
'FJ' => Fiji
'FI' => Finland
'FR' => France
'GF' => French Guiana
'PF' => French Polynesia
'TF' => French Southern Territories
'GA' => Gabon
'GM' => Gambia
'GE' => Georgia
'DE' => Germany
'GH' => Ghana
'GI' => Gibraltar
'GR' => Greece
'GL' => Greenland
'GD' => Grenada
'GP' => Guadeloupe
'GU' => Guam
'GT' => Guatemala
'GG' => Guernsey
'GN' => Guinea
'GW' => Guinea-Bissau
'GY' => Guyana
'HT' => Haiti
'HN' => Honduras
'HK' => Hong Kong
'HU' => Hungary
'IS' => Iceland
'IN' => India
'ID' => Indonesia
'IR' => Iran
'IQ' => Iraq
'IE' => Republic of Ireland
'IM' => Isle of Man
'IL' => Israel
'IT' => Italy
'CI' => Ivory Coast
'JM' => Jamaica
'JP' => Japan
'JE' => Jersey
'JO' => Jordan
'KZ' => Kazakhstan
'KE' => Kenya
'KI' => Kiribati
'KW' => Kuwait
'KG' => Kyrgyzstan
'LA' => Laos
'LV' => Latvia
'LB' => Lebanon
'LS' => Lesotho
'LR' => Liberia
'LY' => Libya
'LI' => Liechtenstein
'LT' => Lithuania
'LU' => Luxembourg
'MO' => Macao S.A.R., China
'MK' => Macedonia
'MG' => Madagascar
'MW' => Malawi
'MY' => Malaysia
'MV' => Maldives
'ML' => Mali
'MT' => Malta
'MH' => Marshall Islands
'MQ' => Martinique
'MR' => Mauritania
'MU' => Mauritius
'YT' => Mayotte
'MX' => Mexico
'FM' => Micronesia
'MD' => Moldova
'MC' => Monaco
'MN' => Mongolia
'ME' => Montenegro
'MS' => Montserrat
'MA' => Morocco
'MZ' => Mozambique
'MM' => Myanmar
'NA' => Namibia
'NR' => Nauru
'NP' => Nepal
'NL' => Netherlands
'AN' => Netherlands Antilles
'NC' => New Caledonia
'NZ' => New Zealand
'NI' => Nicaragua
'NE' => Niger
'NG' => Nigeria
'NU' => Niue
'NF' => Norfolk Island
'KP' => North Korea
'MP' => Northern Mariana Islands
'NO' => Norway
'OM' => Oman
'PK' => Pakistan
'PW' => Palau
'PS' => Palestinian Territory
'PA' => Panama
'PG' => Papua New Guinea
'PY' => Paraguay
'PE' => Peru
'PH' => Philippines
'PN' => Pitcairn
'PL' => Poland
'PT' => Portugal
'PR' => Puerto Rico
'QA' => Qatar
'RE' => Reunion
'RO' => Romania
'RU' => Russia
'RW' => Rwanda
'BL' => Saint Barthélemy
'SH' => Saint Helena
'KN' => Saint Kitts and Nevis
'LC' => Saint Lucia
'MF' => Saint Martin (French part)
'PM' => Saint Pierre and Miquelon
'VC' => Saint Vincent and the Grenadines
'WS' => Samoa
'SM' => San Marino
'ST' => São Tomé and Príncipe
'SA' => Saudi Arabia
'SN' => Senegal
'RS' => Serbia
'SC' => Seychelles
'SL' => Sierra Leone
'SG' => Singapore
'SK' => Slovakia
'SI' => Slovenia
'SB' => Solomon Islands
'SO' => Somalia
'ZA' => South Africa
'GS' => South Georgia/Sandwich Islands
'KR' => South Korea
'ES' => Spain
'LK' => Sri Lanka
'SD' => Sudan
'SR' => Suriname
'SJ' => Svalbard and Jan Mayen
'SZ' => Swaziland
'SE' => Sweden
'CH' => Switzerland
'SY' => Syria
'TW' => Taiwan
'TJ' => Tajikistan
'TZ' => Tanzania
'TH' => Thailand
'TL' => Timor-Leste
'TG' => Togo
'TK' => Tokelau
'TO' => Tonga
'TT' => Trinidad and Tobago
'TN' => Tunisia
'TR' => Turkey
'TM' => Turkmenistan
'TC' => Turks and Caicos Islands
'TV' => Tuvalu
'VI' => U.S. Virgin Islands
'USAF' => US Armed Forces
'UM' => US Minor Outlying Islands
'UG' => Uganda
'UA' => Ukraine
'AE' => United Arab Emirates
'GB' => United Kingdom
'US' => United States
'UY' => Uruguay
'UZ' => Uzbekistan
'VU' => Vanuatu
'VA' => Vatican
'VE' => Venezuela
'VN' => Vietnam
'WF' => Wallis and Futuna
'EH' => Western Sahara
'YE' => Yemen
'ZM' => Zambia
'ZW' => Zimbabwe

62 thoughts on “Add/Modify States for WooCommerce Checkout Form”

  1. Worked for me, thanks. And here’s this if anyone needs it:

    ‘AL’ => ‘ALABAMA’,
    ‘AK’ => ‘ALASKA’,
    ‘AZ’ => ‘ARIZONA’,
    ‘AR’ => ‘ARKANSAS’,
    ‘CA’ => ‘CALIFORNIA’,
    ‘CO’ => ‘COLORADO’,
    ‘CT’ => ‘CONNECTICUT’,
    ‘DE’ => ‘DELAWARE’,
    ‘DC’ => ‘DISTRICT OF COLUMBIA’,
    ‘FL’ => ‘FLORIDA’,
    ‘GA’ => ‘GEORGIA’,
    ‘HI’ => ‘HAWAII’,
    ‘ID’ => ‘IDAHO’,
    ‘IL’ => ‘ILLINOIS’,
    ‘IN’ => ‘INDIANA’,
    ‘IA’ => ‘IOWA’,
    ‘KS’ => ‘KANSAS’,
    ‘KY’ => ‘KENTUCKY’,
    ‘LA’ => ‘LOUISIANA’,
    ‘ME’ => ‘MAINE’,
    ‘MD’ => ‘MARYLAND’,
    ‘MA’ => ‘MASSACHUSETTS’,
    ‘MI’ => ‘MICHIGAN’,
    ‘MN’ => ‘MINNESOTA’,
    ‘MS’ => ‘MISSISSIPPI’,
    ‘MO’ => ‘MISSOURI’,
    ‘MT’ => ‘MONTANA’,
    ‘NE’ => ‘NEBRASKA’,
    ‘NV’ => ‘NEVADA’,
    ‘NH’ => ‘NEW HAMPSHIRE’,
    ‘NJ’ => ‘NEW JERSEY’,
    ‘NM’ => ‘NEW MEXICO’,
    ‘NY’ => ‘NEW YORK’,
    ‘NC’ => ‘NORTH CAROLINA’,
    ‘ND’ => ‘NORTH DAKOTA’,
    ‘OH’ => ‘OHIO’,
    ‘OK’ => ‘OKLAHOMA’,
    ‘OR’ => ‘OREGON’,
    ‘PA’ => ‘PENNSYLVANIA’,
    ‘RI’ => ‘RHODE ISLAND’,
    ‘SC’ => ‘SOUTH CAROLINA’,
    ‘SD’ => ‘SOUTH DAKOTA’,
    ‘TN’ => ‘TENNESSEE’,
    ‘TX’ => ‘TEXAS’,
    ‘UT’ => ‘UTAH’,
    ‘VT’ => ‘VERMONT’,
    ‘VA’ => ‘VIRGINIA’,
    ‘WA’ => ‘WASHINGTON’,
    ‘WV’ => ‘WEST VIRGINIA’,
    ‘WI’ => ‘WISCONSIN’,
    ‘WY’ => ‘WYOMING’

  2. Just an FYI… the states listed has ‘ and ’ in the code above.

    You need to use ‘ instead otherwise the functions.php will break and the site will also break.

    Here’s the same as above with the small change in punctuation.

    ‘AL’ => ‘ALABAMA’,
    ‘AK’ => ‘ALASKA’,
    ‘AZ’ => ‘ARIZONA’,
    ‘AR’ => ‘ARKANSAS’,
    ‘CA’ => ‘CALIFORNIA’,
    ‘CO’ => ‘COLORADO’,
    ‘CT’ => ‘CONNECTICUT’,
    ‘DE’ => ‘DELAWARE’,
    ‘DC’ => ‘DISTRICT OF COLUMBIA’,
    ‘FL’ => ‘FLORIDA’,
    ‘GA’ => ‘GEORGIA’,
    ‘HI’ => ‘HAWAII’,
    ‘ID’ => ‘IDAHO’,
    ‘IL’ => ‘ILLINOIS’,
    ‘IN’ => ‘INDIANA’,
    ‘IA’ => ‘IOWA’,
    ‘KS’ => ‘KANSAS’,
    ‘KY’ => ‘KENTUCKY’,
    ‘LA’ => ‘LOUISIANA’,
    ‘ME’ => ‘MAINE’,
    ‘MD’ => ‘MARYLAND’,
    ‘MA’ => ‘MASSACHUSETTS’,
    ‘MI’ => ‘MICHIGAN’,
    ‘MN’ => ‘MINNESOTA’,
    ‘MS’ => ‘MISSISSIPPI’,
    ‘MO’ => ‘MISSOURI’,
    ‘MT’ => ‘MONTANA’,
    ‘NE’ => ‘NEBRASKA’,
    ‘NV’ => ‘NEVADA’,
    ‘NH’ => ‘NEW HAMPSHIRE’,
    ‘NJ’ => ‘NEW JERSEY’,
    ‘NM’ => ‘NEW MEXICO’,
    ‘NY’ => ‘NEW YORK’,
    ‘NC’ => ‘NORTH CAROLINA’,
    ‘ND’ => ‘NORTH DAKOTA’,
    ‘OH’ => ‘OHIO’,
    ‘OK’ => ‘OKLAHOMA’,
    ‘OR’ => ‘OREGON’,
    ‘PA’ => ‘PENNSYLVANIA’,
    ‘RI’ => ‘RHODE ISLAND’,
    ‘SC’ => ‘SOUTH CAROLINA’,
    ‘SD’ => ‘SOUTH DAKOTA’,
    ‘TN’ => ‘TENNESSEE’,
    ‘TX’ => ‘TEXAS’,
    ‘UT’ => ‘UTAH’,
    ‘VT’ => ‘VERMONT’,
    ‘VA’ => ‘VIRGINIA’,
    ‘WA’ => ‘WASHINGTON’,
    ‘WV’ => ‘WEST VIRGINIA’,
    ‘WI’ => ‘WISCONSIN’,
    ‘WY’ => ‘WYOMING’

  3. Hi there,

    I need to exclude 3 states from Australia. Is the code below correct for excluding those states? Or do I need to include the states I want to keep instead?

    add_filter( ‘woocommerce_states’, ‘custom_woocommerce_states’ );
    function custom_woocommerce_states( $states ) {
    $states[‘AU’] = array(
    ‘NT’ => ‘Northern Territory’,
    ‘WA’ => ‘Western Australia’,
    ‘TAS’ => ‘Tasmania’
    );
    return $states;
    }

  4. Oh Sorry, it was the latter. It wasn’t showing up on my site originally. Just to be sure though, I can have a 3 letter State Reference can’t I?

    1. Hi Jack, you can put below code to your plugin functions.php:


      add_filter( 'woocommerce_states', 'custom_woocommerce_states' );

      function custom_woocommerce_states( $states ) {

      $states['XX'] = array(
      'XX1' => 'State 1',
      'XX2' => 'State 2'
      );

      return $states;
      }

      Just replace XX with the country code your changing.

  5. Hello terrystang. Your article is great. But my question is quiet specific.

    My store is built for India and for one state only which is Delhi.
    So my store country is INDIA.
    And my store is available for only one state ie, DELHI.

    Therefore, I need to exclude all states except DELHI.
    Please give me the code to get my job done.

    Thanks

  6. Michael R. Murphy

    Hi Terry,
    Thanks for the great tutorial. Believe it or not it’s been hard for me to find info on modifying this until I found your post. I’m having trouble finding a filter that modifies *just* the shipping state options however. This code filters both billing and shipping. In my case I don’t care where someone’s billing address is but I only want orders shipping to certain states. Do you know of a way to modify just the ship state using filters?
    MRM

  7. Thank you for this tutorial. I have the same problem like Michael. I would like to restrict shipping, but not billing. Also, do you think it would be possible to have an alert message (that an item cannot be shipped to x) at checkout? Kind regards

  8. Hi there, I’m trying to use this with another function in my client’s site. I’ve tried to add it right underneath this other custom function I need:

    /**
    Add the field to order emails **/

    add_filter(‘woocommerce_email_order_meta_keys’, ‘my_woocommerce_email_order_meta_keys’);
    function my_woocommerce_email_order_meta_keys( $keys ) {
    $keys[‘Delivery Method’] = ‘delivery’;
    return $keys;
    }

    But for some reason it keeps getting me the WordPress white screen of death … I’m no good with PHP, so I’m not sure if I’m missing something between the two add_filters, if they can’t be used together, or if there’s some syntax error somewhere?

    Advice would be really appreciated!

  9. I’d also like to know if there’s a way to restrict shipping to certain states on an individual product level. For instance, Products A, B & C can be shipped to all US States other than CA, while Products D – Z can be shipped to all states. Any ideas to help?

  10. This is very helpful, thanks for sharing. Like Michael and a few others, I am wondering if there’s an easy way to allow a customer to use a restricted state for billing, and ship to a state that’s allowed. Any insight you can share would be great. Thanks!

  11. I think the theme used on this site is breaking the list of lower 48 states by replacing the apostrophe with open and close single quotes. Frustrating to edit manually.

  12. When I tried to upload the codes and my page broke! And when I erased them back to the original codes is still broken. Please tell me there is something I can do. to fix it.

  13. hey guys,

    Does anyone know the 2 letter codes for the New Zealand “states”? I can’t find this info and have managed to work out about half using guess and check. if someone can help i’ll buy you a beer!

    1. Hi david, here is the list for 2 letter codes for New Zealand:

      'NL' => 'Northland',
      'AK' => 'Auckland',
      'WA' => 'Waikato',
      'BP' => 'Bay of Plenty',
      'TK' => 'Taranaki',
      'HB' => 'Hawke’s Bay',
      'MW' => 'Manawatu-Wanganui',
      'WE' => 'Wellington',
      'NS' => 'Nelson',
      'MB' => 'Marlborough',
      'TM' => 'Tasman',
      'WC' => 'West Coast',
      'CT' => 'Canterbury',
      'OT' => 'Otago',
      'SL' => 'Southland'
      
  14. I’m having trouble finding the (function.php) file in the WooCommerce area of Plugins > Editor. Can you state exactly where to find this file through navigating the dashboard?

    1. After login to WordPress Admin, mouse over “Appearance” sidebar menu, and click “Editor” link. After that, do find “functions.php” file on your right sidebar.

  15. @nitin

    Here is the list of Indian States for Woocommerce

    ‘AP’ => __(‘Andra Pradesh’, ‘woocommerce’),
    ‘AR’ => __(‘Arunachal Pradesh’, ‘woocommerce’),
    ‘AS’ => __(‘Assam’, ‘woocommerce’),
    ‘BR’ => __(‘Bihar’, ‘woocommerce’),
    ‘CT’ => __(‘Chhattisgarh’, ‘woocommerce’),
    ‘GA’ => __(‘Goa’, ‘woocommerce’),
    ‘GJ’ => __(‘Gujarat’, ‘woocommerce’),
    ‘HR’ => __(‘Haryana’, ‘woocommerce’),
    ‘HP’ => __(‘Himachal Pradesh’, ‘woocommerce’),
    ‘JK’ => __(‘Jammu and Kashmir’, ‘woocommerce’),
    ‘JH’ => __(‘Jharkhand’, ‘woocommerce’),
    ‘KA’ => __(‘Karnataka’, ‘woocommerce’),
    ‘KL’ => __(‘Kerala’, ‘woocommerce’),
    ‘MP’ => __(‘Madhya Pradesh’, ‘woocommerce’),
    ‘MH’ => __(‘Maharashtra’, ‘woocommerce’),
    ‘MN’ => __(‘Manipur’, ‘woocommerce’),
    ‘ML’ => __(‘Meghalaya’, ‘woocommerce’),
    ‘MZ’ => __(‘Mizoram’, ‘woocommerce’),
    ‘NL’ => __(‘Nagaland’, ‘woocommerce’),
    ‘OR’ => __(‘Orissa’, ‘woocommerce’),
    ‘PB’ => __(‘Punjab’, ‘woocommerce’),
    ‘RJ’ => __(‘Rajasthan’, ‘woocommerce’),
    ‘SK’ => __(‘Sikkim’, ‘woocommerce’),
    ‘TN’ => __(‘Tamil Nadu’, ‘woocommerce’),
    ‘TR’ => __(‘Tripura’, ‘woocommerce’),
    ‘UT’ => __(‘Uttaranchal’, ‘woocommerce’),
    ‘UP’ => __(‘Uttar Pradesh’, ‘woocommerce’),
    ‘WB’ => __(‘West Bengal’, ‘woocommerce’),
    ‘AN’ => __(‘Andaman and Nicobar Islands’, ‘woocommerce’),
    ‘CH’ => __(‘Chandigarh’, ‘woocommerce’),
    ‘DN’ => __(‘Dadar and Nagar Haveli’, ‘woocommerce’),
    ‘DD’ => __(‘Daman and Diu’, ‘woocommerce’),
    ‘DL’ => __(‘Delhi’, ‘woocommerce’),
    ‘LD’ => __(‘Lakshadeep’, ‘woocommerce’),
    ‘PY’ => __(‘Pondicherry (Puducherry)’, ‘woocommerce’)

  16. I’m surprised that this info has been so difficult to find. All I want to do is ship flat rate, but charge more to Alaska and Hawaii. Seems like it would be a common requirement, and not much to ask!
    But for now just preventing shipping to these areas will have to suffice.
    So my problem is the same as several above mentioned. The item I sell is a gift item, so the shipping and billing addresses will be different. Therefore I need to restrict shipping only to those states.

  17. Hello,
    I am shipping in Canada and US. When user selects “Canada” at the checkout page I want to display States in Canada in “States” field of Checkout page.
    How can I do that?

  18. Hi! I have a similar problem, I thought this would fix it, but not this time…
    In my shipping admin area, I have all the cities for
    Brazil (for example Barzil > Rio de Janeiro)
    Spain (for example Spain > Madrid)
    Italy (for example italia > Milan)
    But not city list for other countries. Do you know haw can I add them?
    thanks!

  19. Hi,

    We added a custom state list for a country, and everything seems to be working fine. But the “State field” for this new country is not showing as a “required” field.

    How can achieve this? So customers have to select the state for this new country?

    Thanks!

    1. Hi Mohamed, you can add below code into your theme functions.php to make state field required:

      add_filter( 'woocommerce_default_address_fields' , 'tt_override_default_address_fields' );
      
      function tt_override_default_address_fields( $address_fields ) {
           $address_fields['state']['required'] = false;
      
           return $address_fields;
      }
      
  20. Hi even it didn’t work for me I’am based in India and would like to restrict shipping to ‘Delhi’ only but I’m unable to achieve this I also tried other sources but unfortunately. I can even pay for your help please reply this is important.

    I’m using woocommerce version 2.2.8.

    1. Try this.

      function tt_only_ship_to_delhi( $available_methods ) {
        global $woocommerce;
        $included_states = array( 'DL' );
        if( !in_array( $woocommerce->customer->get_shipping_state(),$included_states ) ) {
          // Empty the $available_methods array
          $available_methods = array();
        }
        return $available_methods;
      }
      
      add_filter( 'woocommerce_package_rates', 'tt_only_ship_to_delhi', 10);
  21. Hey All,

    Trying to have a custom state list that leaves out places like Virgin Islands, etc. We only ship to US.

    Can someone give me the correct list for all US states in the proper syntax? When I use the one Chris J. Popp posted, it is giving my syntax errors.

    Please and thank you

  22. Regarding my last comment. I figured out the syntax error was due to the spaces between the names of the states like ‘DISTRICT OF COLUMBIA’ ‘NEW HAMPSHIRE’ etc. How do i make it display a space and have the syntax correct? (Tried &nbps; already with no luck) As you can probably gather, I am new to php

  23. Hi terrytsang,
    The code I used is:

    add_filter( ‘woocommerce_states’, ‘custom_woocommerce_states’ );
    function custom_woocommerce_states( $states ) {
    $states[‘US’] = array(
    ‘AL’ => ‘ALABAMA’,
    ‘AK’ => ‘ALASKA’,
    ‘AZ’ => ‘ARIZONA’,
    ‘AR’ => ‘ARKANSAS’,
    ‘CA’ => ‘CALIFORNIA’,
    ‘CO’ => ‘COLORADO’,
    ‘CT’ => ‘CONNECTICUT’,
    ‘DE’ => ‘DELAWARE’,
    ‘DC’ => ‘DISTRICT OF COLUMBIA’,
    ‘FL’ => ‘FLORIDA’,
    ‘GA’ => ‘GEORGIA’,
    ‘HI’ => ‘HAWAII’,
    ‘ID’ => ‘IDAHO’,
    ‘IL’ => ‘ILLINOIS’,
    ‘IN’ => ‘INDIANA’,
    ‘IA’ => ‘IOWA’,
    ‘KS’ => ‘KANSAS’,
    ‘KY’ => ‘KENTUCKY’,
    ‘LA’ => ‘LOUISIANA’,
    ‘ME’ => ‘MAINE’,
    ‘MD’ => ‘MARYLAND’,
    ‘MA’ => ‘MASSACHUSETTS’,
    ‘MI’ => ‘MICHIGAN’,
    ‘MN’ => ‘MINNESOTA’,
    ‘MS’ => ‘MISSISSIPPI’,
    ‘MO’ => ‘MISSOURI’,
    ‘MT’ => ‘MONTANA’,
    ‘NE’ => ‘NEBRASKA’,
    ‘NV’ => ‘NEVADA’,
    ‘NH’ => ‘NEW HAMPSHIRE’,
    ‘NJ’ => ‘NEW JERSEY’,
    ‘NM’ => ‘NEW MEXICO’,
    ‘NY’ => ‘NEW YORK’,
    ‘NC’ => ‘NORTH CAROLINA’,
    ‘ND’ => ‘NORTH DAKOTA’,
    ‘OH’ => ‘OHIO’,
    ‘OK’ => ‘OKLAHOMA’,
    ‘OR’ => ‘OREGON’,
    ‘PA’ => ‘PENNSYLVANIA’,
    ‘RI’ => ‘RHODE ISLAND’,
    ‘SC’ => ‘SOUTH CAROLINA’,
    ‘SD’ => ‘SOUTH DAKOTA’,
    ‘TN’ => ‘TENNESSEE’,
    ‘TX’ => ‘TEXAS’,
    ‘UT’ => ‘UTAH’,
    ‘VT’ => ‘VERMONT’,
    ‘VA’ => ‘VIRGINIA’,
    ‘WA’ => ‘WASHINGTON’,
    ‘WV’ => ‘WEST VIRGINIA’,
    ‘WI’ => ‘WISCONSIN’,
    ‘WY’ => ‘WYOMING’
    );
    return $states;
    }

    It is saying syntax error for every state that has a space in the value (District of Columbia, New Hampshire, New Jersey, etc.)

  24. I have try your code
    function tt_only_ship_to_illinois( $available_methods ) {
    global $woocommerce;
    $included_states = array( ‘IL’ );
    if( !in_array( $woocommerce->customer->get_shipping_state(),$included_states ) ) {
    // Empty the $available_methods array
    $available_methods = array();
    }
    return $available_methods;
    }

    add_filter( ‘woocommerce_package_rates’, ‘tt_only_ship_to_illinois’, 10);

    but it looks like is not working with WooCommerce 2.2.11
    Any clue?

  25. Hello Terry. I like your article and i have a specific question, maybe you can help me here.

    When i go to the checkout out page and enter all the billing information for let’s say the country ‘germany’, it works fine but as soon as i change to e.g. china it doesn’t allow me to purchase because i get an error saying ‘province’ is a mandatory field but its missing in my checkout form. I get that field only when i refresh the page. Do you have any hints or solutions to this problem?
    In woocommerce i set it up to ‘all countries’ and its a digital downloadable product.

    thank you!

    1. Hi Albert, you might need to change to default theme and have a try again, i suspected the bugs mostly due to conflict between your current theme plugins and WooCommerce ajax populating province/state field when select Country.

      Or else you just make Province/State field not required, then the Required validation will skip this field and not produce any error also.

  26. Any idea about afghanistan.. when i select it my state dropdown is not showing on and it changes to design also..

  27. Hello,

    this method works fine as far as shipping address is concerned. However, my clients can be anywhere in the world, therefore I need all states in billing address. Can this be done?

  28. Hi. My problem is similar, but i need put the state and cities for shipping my product in colombia. Is possible?

  29. Hello All,
    Here is how to extract unwanted U.S. territories not in the continental U.S… (although this also prevents users from buying from these territories as well)… the credit for this code goes to:
    http://jboullion.com/set-woocommerce-state/
    =========
    /* Restricting shipping only to continental U.S. by removing certain states from default U.S. list */
    add_filter( ‘woocommerce_states’, ‘custom_woocommerce_states’ );

    function custom_woocommerce_states( $states ) {
    $excluded_states = array(‘AK’, ‘AA’, ‘AE’, ‘AP’, ‘AS’, ‘GU’, ‘MP’, ‘PR’, ‘UM’, ‘VI’, ‘HI’);
    foreach($excluded_states as $no_state){
    unset($states[‘US’][$no_state]);
    }

    return $states;
    }

  30. Hello terrytsang your post is great!
    But i am facing very serious problem, i lived in Pakistan and i only want to modify all States and Cities in Pakistan, because i start an online shopping Store which sells branded products all over the Pakistan not foreign countries, So kindly tell me how to add only Pakistan’s States and its cities. Shipping rates are variable on different cities…

  31. hi,
    can any one tell me how can i get Full state name ?
    it pass iso state code to payment gateway so i need to use full name of state.

    or

    how can i get full name of state other than two digit iso code.

  32. Hi Terry,

    I’ve tried to follow your instructions and added the script in my functions.php but it turned out to have a Server Error 500. Do you have any clues about this?

    Sorry for the very newbie question as I’m a complete greenhorn.

  33. Hey, would anybody know how to set it to a specific city on the checkout page? Setting it to states works great but I’m struggling with setting it to just one city. Thank you!

  34. This is excellent! Thank you!

    The syntax errors from copying and pasting the nice list of 50 United States might be coming from the educated single quote marks.

    This is what I used:

    'AL' => 'Alabama',
    'AK' => 'Alaska',
    'AZ' => 'Arizona',
    'AR' => 'Arkansas',
    'CA' => 'California',
    'CO' => 'Colorado',
    'CT' => 'Connecticut',
    'DE' => 'Delaware',
    'DC' => 'District Of Columbia',
    'FL' => 'Florida',
    'GA' => 'Georgia',
    'HI' => 'Hawaii',
    'ID' => 'Idaho',
    'IL' => 'Illinois',
    'IN' => 'Indiana',
    'IA' => 'Iowa',
    'KS' => 'Kansas',
    'KY' => 'Kentucky',
    'LA' => 'Louisiana',
    'ME' => 'Maine',
    'MD' => 'Maryland',
    'MA' => 'Massachusetts',
    'MI' => 'Michigan',
    'MN' => 'Minnesota',
    'MS' => 'Mississippi',
    'MO' => 'Missouri',
    'MT' => 'Montana',
    'NE' => 'Nebraska',
    'NV' => 'Nevada',
    'NH' => 'New Hampshire',
    'NJ' => 'New Jersey',
    'NM' => 'New Mexico',
    'NY' => 'New York',
    'NC' => 'North Carolina',
    'ND' => 'North Dakota',
    'OH' => 'Ohio',
    'OK' => 'Oklahoma',
    'OR' => 'Oregon',
    'PA' => 'Pennsylvania',
    'RI' => 'Rhode Island',
    'SC' => 'South Carolina',
    'SD' => 'South Dakota',
    'TN' => 'Tennessee',
    'TX' => 'Texas',
    'UT' => 'Utah',
    'VT' => 'Vermont',
    'VA' => 'Virginia',
    'WA' => 'Washington',
    'WV' => 'West Virginia',
    'WI' => 'Wisconsin',
    'WY' => 'Wyoming'

  35. Hi! What would be the needed code if i need to Add new states?
    I live in a country not included in Woocommerce (the Country is Chile, but no states are included) and i would like to add them.

    Sates are up to 15, and different cities are almost 300.
    Is it possible?
    Thank you!!!

  36. Same question here!! I’m in Bolivia a no cities appear to have added to the WooCommerce database. I just need to create different shipping zones for each city. Is it possible to create (associate) cities or states to a specific country?

    Thanks!!

  37. Thanks, this work perfectly for me. But when the state is been selected more than 2 times it freeze the checkout page(for instance when a customer made a mistake in selecting a different page multiple times), you have to restart the page all over again.

  38. Is there anyway to replace ‘Select an option’ with a state name from woocommerce checkout page. Also needed to show other state when click the select box

Leave a Comment

Your email address will not be published. Required fields are marked *