This post shows you how to include only specific states that can purchase from your WooCommerce website. The code below is a modified version that includes a full list of U.S. states. To limit sales to customers in specific states, remove the states you do not want to sell to, making the list consist only of states you will sell to.
<php
/**
* Sell only in specific states
* Remove the states not eligible to purchase
* Final list is of states that CAN purchase
*/
add_filter( 'woocommerce_states', 'wc_sell_only_states' );
function wc_sell_only_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;
}
This worked perfectly for me! Thank you! 🙂 🙂
Excellent! Thanks for letting us know. 🙂
I need to block only certain products from being sold to certain states. For instance one product can’t be sold to California. However, different products can be sold to California.
I wish there was a way in the products section I could choose that.
Hello, Jeff. The Conditional Shipping and Payments modules available with the UltimateWoo plugin should cover what you need.
Perfect work Thanks 🙂
is there a way in the ship to a different address shipping state field that it shows only 2 states in that field? I still want the user to be able to select any state from the billing fields.
Thanks!
Where do I enter the code?
Functions.php, or a custom function plugin.
thank you!
My client wants the customer to get a message if they try to ship to other than the lower 48 in the US. Is there a way to do this? I’ve tried the Advanced Shipping Validation for Woocommerce, and it doesn’t work as expected.
Hi Cindy,
So you want to display a message to the custom if he or she is trying to have their order shipped to the lower 48? Should this happen before checkout is completed, serve as an error, or something else? Basically, what’s the purpose of the message? This would help us understand what you’re trying to do.
I want the customer to get a message if they try to ship to other than the lower 48 in the US. In other words I provide free shipping to the lower 48 only. I don’t give free shipping to AK, HI, the VI, PR, Guam or to Canada If the customer is in one of those non free places I want to be able to give the customer a message to telling them to call us at 1-800-555-1212 to get the shipping costs to ship to them. Is there a way to do this?
Yes, but you’ll need some custom development for this, since there’s no built-in method for handling it.
Can I restrict selling all my products for customers with certain zip codes? 🙂 Eric