If you are reading this article then you must have been looking for a solution to sell your products to specific states or limit states in checkout in your WooCommerce site.

In the WooCommerce default setting, you can set rules to sell your products to specific countries or limit selling by zip codes only.

Unfortunately, the default setting doesn’t allow you to limit your selling to specific states/districts. So, what if you want to modify your WooCommerce states/districts list to sell to specific locations?

Well, here is the solution…

How to Sell to Specific States / Districts in WooCommerce

Using the PHP snippet below, you can override the default states/districts in WooCommece to limit your selling to specific states for a particular country.

/**
* @snippet Sell to Specific States / Districts in WooCommerce
* @source https://www.wptechnic.com/?p=6919
* @compatible WC 6.3.1
*/
add_filter( 'woocommerce_states', 'wptechnic_woocommerce_states' );

function wptechnic_woocommerce_states( $states ) {

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

  return $states;
}

NOTE: You must replace both instances of “XX” from the code above with your own country code. This means each state id in the array must have your two-letter country code before the number you assign to the state.

So for example, if your country is the United States, you have to replace the “XX” with “US“. And after making the changes, the code will look like this:

/**
* @snippet Modify or Add Custom States / District in WooCommerce
* @source https://www.wptechnic.com/?p=6919
* @compatible WC 6.3.1
*/
add_filter( 'woocommerce_states', 'wptechnic_woocommerce_states' );

function wptechnic_woocommerce_states( $states ) {

  $states['US'] = array(
    'US1' => 'New Texas', 
    'US2' => 'New Florida'
  );

  return $states;
}

To show you an example, we have added the code to override the state list for the US with 2 chosen states only, New Texas and New Florida.

Sell to Specific States Districts in WooCommerce

And here is the result:

How to Add Custom States Districts WooCommerce

How to add PHP Snippet code to your WordPress site?

There are many ways of adding a PHP Snippet code to your WordPress website. You can add this PHP Snippet at the very bottom of your active child theme (or main theme) functions.php file. You can also add this code using any third-party plugin like the ‘Insert Headers and Footers‘ or ‘Code Snippets‘ plugin.

Conclusion

This code is suitable if you operate your business locally or allow shipping to specific states in a country.

Did this code work for your website? Or you’re having difficulties? Either way, let me know by leaving a comment below right now.

Related Posts:

  1. WooCommerce – List of supported Country Codes and State Codes
Avatar for Muhammad Tamzid
Author

Muhammad Tamzid, the founder of WPTechnic, is a WordPress Enthusiast with a passion to help beginners learning WordPress. Also managing WPrevival, a 24/7 WordPress Website Development, Maintenance & Security Service company.

Write A Comment