How to Add or Modify State in WooCommerce?

Updated on September 27, 2017

Do you need to add or modify state in WooCommerce plugin? If yes, all you need is a simple snippet. Generally, every country in WooCommerce has an unique code. For example, India has a country code of ‘IN’ and IRAN as ‘IR’. Here’s a complete list of country codes in WooCommerce plugin.

To add a new state or modify a state of a particular country, you need to use add_filter hook with a callback function.

add_filter is a hook that allows plugins to modify internal data during the runtime.

modify state in WooCommerce

How to add or modify state in WooCommerce?

Copy and paste the below hook in functions.php (located in theme directory)

add_filter( 'woocommerce_states', 'custom_woocommerce_states' );
function custom_woocommerce_states( $states ) {
$states['XX'] = array(
'XX1' => 'State 1',
'XX2' => 'State 2'
);
return $states;
}

Note: In the above snippet, you need to replace ‘XX’ with a country code for which you are adding or modifying the state.

Assume, you want to add “Dubai” to “United Arab Emirates”, then the code will look as shown below:

add_filter('woocommerce_cart_calculate_fees1' , 'add_discount',10,1);
add_filter( 'woocommerce_states', 'custom_woocommerce_states' );
function custom_woocommerce_states( $states ) {
$states['AE'] = array(
'XX1' => 'Dubai',
);
return $states;
}

In the above code, lookout for country code ‘AE’ for United Arab Emirates.

The above snippet was referenced from official WooCommerce documentation.

Also Read: Customize States displayed in WooCommerce checkout form.

Was this article helpful?

Related Articles

Comments Leave a Comment

  1. Can one use radio buttons for state in place of select fields

    1. This tutorial is to add the states which are missing in the woocommerce. Once added, it will show as a dropdown on the checkout page. You can style it accordingly to make it a radio button.

Leave a Comment