How to Restrict Shipping to Only One State in WooCommerce?

At times, you wish to restrict product delivery areas to certain states. In such scenarios, many want to allow product billing the same as before. However, they want limited shipping restricted to one state. In this article, you will learn to restrict shipping to only one state. 

Let’s see how it is done:

PHP Snippet 1: WooCommerce Allow Shipping to Only One State at Cart Shipping Calculator

add_filter( 'woocommerce_states', 'phpsof_cart_filter_US_states' );
  
function phpsof_cart_filter_US_states( $states ) {
if ( is_cart() ) {
   $states['US'] = array(
      'PA' => 'Pennsylvania',
   );
}
return $states;
}

Image showing how to restrict shipping to only one state at cart shipping calculator

PHP Snippet 2: Limit Shipping to Only One State at Checkout Page

Please remember that this snippet works when you have limited shipping to a specific country. 

add_filter( 'woocommerce_countries_shipping_country_states', 'phpsof_set_checkout_shipping_state' );
   
function phpsof_set_checkout_shipping_state( $states ) {
   $states[ 'US' ] = array( 'PA' => __( 'Pennsylvania', 'woocommerce' ) );
     return $states;
}

In certain cases, it can also restrict billing. To resolve this error, you can use the below-provided alternative snippet. Applying this snippet, you can allow delivery for one state while billing is allowed same as before. 

add_action( 'wp_footer', 'phpsof_checkout_shipping_filter_US_states' );
   
function phpsof_checkout_shipping_filter_US_states() {
   if ( ! is_checkout() ) {
      return;
   }
   ?>
      
   <script>
   jQuery(document).ready(function($) {
 
      $(document.body).on('country_to_state_changed', function() {
 
         function set_shipping_state(state) {
            var $shipping_state = $('#shipping_state');
            var $shipping_state_option = $('#shipping_state option[value="' + state + '"]');
            var $shipping_state_option_no = $('#shipping_state option[value!="' + state + '"]');
            $shipping_state_option_no.remove();
            $shipping_state_option.attr('selected', true);
         }
 
         var $shipping_country = $('#shipping_country');
 
         var new_shipping_state = '';
 
         switch($shipping_country.val()) {
            case 'US':
               new_shipping_state = 'PA';
               break;
         }
 
         if( ! $.isEmptyObject(new_shipping_state)) {
            set_shipping_state(new_shipping_state);
         } 
 
      });
 
   });  
   </script>
       
   <?php
}

Image showing how to limit shipping for specific state at WooCommerce checkout page

Where do you add this snippet?

So, this is how you can restrict shipping to only one state in WooCommerce. Also, it is easier to activate this function with this code. So, to apply this code, just add this PHP snippet at your child theme function.php file’s bottom. It should be placed before “?>”, if you have it there. Apart from this, CSS goes in your child theme style.css file. Make sure that you are editing these files in a right manner for best results.

Is this snippet still valid?

So, this is how you can learn about restrict shipping to only one state in WooCommerce. I have applied this code on the Storefront theme and WordPress friendly hosting PHP 7.3. Let me know if everything works as expected when you code. Share it further if you find this snippet useful for you and it had saved your time. 

Also Read, How to Redirect to Custom Thank You Page in WooCommerce?

How to Add Equal Product Heights at Shop Page in WooCommerce?

Important links: WooCommerce – https://woocommerce.com/