block certain states in woocommerce

How to Block Certain States in WooCommerce

You have something to sell but not want to sell it to every state in your country for logistical, legal or other reasons?

Well, In such a scenario you can just block certain states in WooCommerce.

Block certain States in WooCommerce

Let me explain, how you can block, restrict or allow some states in any country you sell…

The code below shows the complete list of states in the united states.

Now, in order to block sales in certain states, you need to delete the states you don’t want to sell.

In other words, making the list only consist of the states you sell to. Simple as that…


/**
 * Sell only in specific states
 * Remove the states not eligible to purchase
 * Final list is of states that CAN purchase
 */
add_filter( 'woocommerce_states', 'wpglorify_restrict_states' );
function wpglorify_restrict_states( $states ) {

	$states['US'] = array(
		'AL' => __( 'Alabama', 'woocommerce' ),
		'AK' => __( 'Alaska', 'woocommerce' ),
		'AZ' => __( 'Arizona', 'woocommerce' ),
		'AR' => __( 'Arkansas', 'woocommerce' ),
		'CA' => __( 'California', 'woocommerce' ),
		'CO' => __( 'Colorado', 'woocommerce' ),
		'CT' => __( 'Connecticut', 'woocommerce' ),
		'DE' => __( 'Delaware', 'woocommerce' ),
		'DC' => __( 'District Of Columbia', 'woocommerce' ),
		'FL' => __( 'Florida', 'woocommerce' ),
		'GA' => _x( 'Georgia', 'US state of Georgia', 'woocommerce' ),
		'HI' => __( 'Hawaii', 'woocommerce' ),
		'ID' => __( 'Idaho', 'woocommerce' ),
		'IL' => __( 'Illinois', 'woocommerce' ),
		'IN' => __( 'Indiana', 'woocommerce' ),
		'IA' => __( 'Iowa', 'woocommerce' ),
		'KS' => __( 'Kansas', 'woocommerce' ),
		'KY' => __( 'Kentucky', 'woocommerce' ),
		'LA' => __( 'Louisiana', 'woocommerce' ),
		'ME' => __( 'Maine', 'woocommerce' ),
		'MD' => __( 'Maryland', 'woocommerce' ),
		'MA' => __( 'Massachusetts', 'woocommerce' ),
		'MI' => __( 'Michigan', 'woocommerce' ),
		'MN' => __( 'Minnesota', 'woocommerce' ),
		'MS' => __( 'Mississippi', 'woocommerce' ),
		'MO' => __( 'Missouri', 'woocommerce' ),
		'MT' => __( 'Montana', 'woocommerce' ),
		'NE' => __( 'Nebraska', 'woocommerce' ),
		'NV' => __( 'Nevada', 'woocommerce' ),
		'NH' => __( 'New Hampshire', 'woocommerce' ),
		'NJ' => __( 'New Jersey', 'woocommerce' ),
		'NM' => __( 'New Mexico', 'woocommerce' ),
		'NY' => __( 'New York', 'woocommerce' ),
		'NC' => __( 'North Carolina', 'woocommerce' ),
		'ND' => __( 'North Dakota', 'woocommerce' ),
		'OH' => __( 'Ohio', 'woocommerce' ),
		'OK' => __( 'Oklahoma', 'woocommerce' ),
		'OR' => __( 'Oregon', 'woocommerce' ),
		'PA' => __( 'Pennsylvania', 'woocommerce' ),
		'RI' => __( 'Rhode Island', 'woocommerce' ),
		'SC' => __( 'South Carolina', 'woocommerce' ),
		'SD' => __( 'South Dakota', 'woocommerce' ),
		'TN' => __( 'Tennessee', 'woocommerce' ),
		'TX' => __( 'Texas', 'woocommerce' ),
		'UT' => __( 'Utah', 'woocommerce' ),
		'VT' => __( 'Vermont', 'woocommerce' ),
		'VA' => __( 'Virginia', 'woocommerce' ),
		'WA' => __( 'Washington', 'woocommerce' ),
		'WV' => __( 'West Virginia', 'woocommerce' ),
		'WI' => __( 'Wisconsin', 'woocommerce' ),
		'WY' => __( 'Wyoming', 'woocommerce' ),
		'AA' => __( 'Armed Forces (AA)', 'woocommerce' ),
		'AE' => __( 'Armed Forces (AE)', 'woocommerce' ),
		'AP' => __( 'Armed Forces (AP)', 'woocommerce' ),
		'AS' => __( 'American Samoa', 'woocommerce' ),
		'GU' => __( 'Guam', 'woocommerce' ),
		'MP' => __( 'Northern Mariana Islands', 'woocommerce' ),
		'PR' => __( 'Puerto Rico', 'woocommerce' ),
		'UM' => __( 'US Minor Outlying Islands', 'woocommerce' ),
		'VI' => __( 'US Virgin Islands', 'woocommerce' ),
	);

	return $states;

}

Sell to only one State

This is an example of the above code. Let’s say you are selling fresh produce and only want to sell to your home state.

Because you are selling fresh produce such as fruits, vegetables and don’t have a proper delivery system in place to deliver all over the country.

let’s say you are from California state for this example, with the code below you can remove every other state except California.

// Only one state. example California in the US
 
add_filter( 'woocommerce_states', 'wpglorify_restrict_states' );

function wpglorify_restrict_states( $states ) {

  $states['US'] = array(
  'CA' => __( 'California', 'woocommerce' ),
);
return $states;
}

Now when customers go to the checkout page they will only see California in address drop-down options and not other states.


Restrict States from any other country

This solution is not only for the United States, No matter from which country you are, you only need to do replace country and state names and codes.

Here is an example of Australia

/**
 * list of Australian states to sell
 */

add_filter( 'woocommerce_states', 'wpglorify_restrict_states' );

function wpglorify_restrict_states( $states ) {

	$states['AU'] = array(
		'ACT' => __( 'Australian Capital Territory', 'woocommerce' ),
		'NSW' => __( 'New South Wales', 'woocommerce' ),
		'NT' => __( 'Northern Territory', 'woocommerce' ),
		'QLD' => __( 'Queensland', 'woocommerce' ),
		'SA' => __( 'South Australia', 'woocommerce' ),
		'TAS' => __( 'Tasmania', 'woocommerce' ),
		'VIC' => __( 'Victoria', 'woocommerce' ),
		'WA' => __( 'Western Australia', 'woocommerce' ),

	);
	return $states;
}

I have compiled the list of almost every country code and also included states of some countries for you to find your country and state codes here.

HOW TO ADD PHP CODE

The Code Snippets plugin makes it very easy to not just add PHP snippets to your website but also manage all the snippets you add.

You can activate and deactivate certain snippets, and even adds notes about what they do. It even has better error handling to avoid the PHP error scenarios.

To install it, simply go to Plugins > Add New and search for Code Snippets.

The traditional or normal way to add PHP snippets to your theme is to add directly in your theme’s functions.php file.

However, make sure you are using a child theme otherwise you will lose all changes once you update your theme.

3 Comments

  1. Woohoo….! it works. I have been struggling to limit sales to only few states and this was exactly what I was looking for. Thanks

  2. Hello. I am trying to limit shipping to certain states on the shipping form at checkout page. Example. I cannot ship to California but I would like California addresses to be accepted for billing.
    I would really appreciate any help.

Leave a Reply

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