In this tutorial we’ll learn how to restrict shipping to specific states in Woocommerce. By default customer have all the state options where they can ship to.
To do this add the following lines of code at the end of your theme’s functions.php file:
add_filter( 'woocommerce_states', 'custom_woocommerce_states' ); function custom_woocommerce_states( $states ) { $states['US'] = array( 'PA' => __( 'Pennsylvania', 'woocommerce' ), 'CA' => __( 'California', 'woocommerce' ), 'FL' => __( 'Florida', 'woocommerce' ), 'MD' => __( 'Maryland', 'woocommerce' ) ); return $states; }
You can easily add or remove states in the code.
Now do the following steps:
Add product to cart and move to cart page
Now proceed to checkout.
Here on the checkout page if you select any state that is not listed in the code then you’ll receive an error.