How to Customize States displayed in WooCommerce Checkout Form?

Updated on September 27, 2017

I was working on an ecommerce theme based on WooCommerce plugin, where I need to remove certain Indian states from checkout form. The reason, few courier services do not ship products to certain states. So it’s better to prevent users from selecting those states during the checkout. If you are looking to customize the list of states that are automatically populated in WooCommerce checkout form, then here’s how you can do that.

There are couple of ways in which you can add or modify the list of states. The first method is by commenting out the state names directly in the WooCommerce core file (the file that populates the states/county field in the checkout form) and the second method is by creating a custom function in theme’s functions.php file.

Modify states in woocommerce

Method 1: Comment out States in WooCommerce core file

Step 1: Navigate to WordPress Plugins folder and ‘cd’ to woocommerce.

$ cd woocommerce
$ cd i18n/states
$ vim IN.php

Comment out the states that you wish to remove from the checkout form. For example:

$states['IN'] = array(
/*'AP' => __( 'Andhra 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' ),
'TS' => __( 'Telangana', 'woocommerce' ),
'TR' => __( 'Tripura', 'woocommerce' ),
'UK' => __( 'Uttarakhand', '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' )
);

In this method, we are editing the WooCommerce core file, but the ideal way is to create a custom function in theme’s functions.php file.

Method 2: Custom function to override States

Copy and paste the below function in theme’s functions.php file.

add_filter( 'woocommerce_states', 'custom_woocommerce_states' );

function custom_woocommerce_states( $states ) {
$states['IN'] = array(
'JK' => 'Jammu and Kashmir',
'JH' => 'Jharkhand',
'KA' => 'Karnataka',
'KL' => 'Kerala',
'MP' => 'Madhya Pradesh',
'MH' => 'Maharashtra',
'MN' => 'Manipur',
'ML' => 'Meghalaya',
'MZ' => 'Mizoram',
'NL' => 'Nagaland',
'OR' => 'Orissa',
'PB' => 'Punjab',
'RJ' => 'Rajasthan',
'SK' => 'Sikkim',
'TN' => 'Tamil Nadu',
'TS' => 'Telangana',
'TR' => 'Tripura',
'UK' => 'Uttarakhand',
'UP' => 'Uttar Pradesh',
'WB' => 'West Bengal',
'AN' => 'Andaman and Nicobar Islands',
'CH' => 'Chandigarh',
'DN' => 'Dadar and Nagar Haveli',
'DD' => 'Daman and Diu',
'DL' => 'Delhi',
'LD' => 'Lakshadeep',
'PY' => 'Pondicherry'
);

return $states;
}

In the above function, remove the states that you do not wish to appear in checkout form and this will override the states defined in WooCommerce core file.

That’s it! Just don’t go way, you might want to know how to add or modify state in WooCommerce?

Was this article helpful?

Related Articles

Comments Leave a Comment

Leave a Comment