Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Limit Shipping to Only One State With WooCommerce?

WooCommerce settings allow you to set up shipping classes, options, and zones such that it covers 80 to 90% of your shipping requirements. But what if you want to ship within your own state only? So you would want to restrict shipping to a specific state, and thus handle only WooCommerce local deliveries on your store. It is easy to simplify the checkout process by allowing only one state to be displayed in the state dropdown list.

This post guides you on how to restrict shipping to a single state in the WooCommerce cart’s shipping calculator and the shipping section of the WooCommerce checkout page.

Where to Add Custom Code in WooCommerce?

It is advisable to add the code snippets to the functions.php file of your child theme. Access the file directly from Appearance->Theme File Editor->Locating the child theme’s functions.php from the right sidebar. You can also access it from your theme’s directory file. Insert the following code snippet in functions.php. The alternative & easy option is to install & activate the Code Snippets plugin. You can then add the code as a new snippet via the plugin.

Preliminary Steps

Determine the target country and state(s) for which you want to modify the default state on the cart page and checkout page.

How to Limit Shipping to Only One State With WooCommerce? - Tyche Softwares

Planning to add a LOCAL PICKUP option or let the user choose their delivery date on your WooCommerce store?

Meet Order Delivery Date Pro that lets you set delivery date and time based on product category, shipping methods, pickup methods etc.

Let your customer choose a convenient delivery date and time set by you so that you can handle order fulfillment without any worries. It’s a win-win for both.

Solution 1: Limit shipping to only one state on the WooCommerce Cart Shipping Calculator

Let’s consider an online store that targets customers in a particular region. The below code displays only the ‘New York’ state under the dropdown of the ‘State Field’.

// Add a filter to modify the list of states for the United States
add_filter('woocommerce_states', 'ts_override_woocommerce_states');
function ts_override_woocommerce_states($states) {
   $states['US'] = array(
       'NY' => 'New_York',
       // Add more states as needed
   );
   return $states;
}

Cart Page

The code shows ‘New York (NY)’ as the only shipping state option when ‘United States (US)’ is selected in the state/country dropdown on the cart’s shipping calculator.

How to Limit Shipping to Only One State With WooCommerce? - Tyche Softwares

Solution 2: Limit Shipping to One State in Shipping Section on WooCommerce Checkout Page

The code will restrict the shipping section of the state field as the only shipping state option when ‘United States (US)’ is selected in the country/state dropdown on the WooCommerce checkout page.

// Use JavaScript to update the selected shipping state based on the chosen country
add_action('wp_footer', 'ts_set_shipping_state');
function ts_set_shipping_state() {
    if (is_checkout()) {
        ?>
        <script>
            jQuery(document).ready(function($) {
                $(document.body).on('country_to_state_changed', function() {
                    var selectedCountry = $('#shipping_country').val();
                    var newState = '';
                    
                    // Define the mapping of countries to states here
                    switch (selectedCountry) {
                        case 'US':
                            newState = 'NY'; // New York
                            break;
                        // Add more cases as needed
                    }

                    if (newState) {
                        var $shippingState = $('#shipping_state');
                        var $stateOption = $shippingState.find('option[value="' + newState + '"]');
                        
                        // Remove non-matching states from only the shipping state dropdown
                        $shippingState.find('option').not($stateOption).remove();
                        $stateOption.prop('selected', true);
                        $shippingState.change();
                    }
                });
            });
        </script>
        <?php
    }
}

Checkout Page

The code shows ‘New York (NY)’ as the only shipping state option when ‘United States (US)’ is selected in the country dropdown on the shipping section of the checkout page.

How to Limit Shipping to Only One State With WooCommerce? - Tyche Softwares

Code Explanation

  • The add_filter function is used to attach the ts_override_woocommerce_states function to the woocommerce_states hook and when it is triggered, causes the function to modify the list of states available in WooCommerce for various countries.
  • The function ts_override_woocommerce_states($states) is defined to handle the modification of the states array.
  • $states[‘US’] = array( ‘NY’ => ‘New York’, // Add more states as needed ); This line of code modifies the states array. Specifically, it adds or overrides the states for the United States (‘US’). In this example, it sets ‘NY’ as the key and ‘New York’ as the value, effectively adding ‘New York’ as a state option for the United States.
  • return $states; Finally, the function returns the modified $states array, which will be used to update the list of available states for the United States in the shipping address form.
  • add_action(‘wp_footer’, ‘ts_set_shipping_state’);  adds an action to the ‘wp_footer’ hook, which is triggered when the footer of a WordPress page is being generated. When this hook is triggered, it calls the function ts_set_shipping_state to include JavaScript in the footer of the page.
  • The function ts_set_shipping_state()  is defined to handle the JavaScript code that dynamically sets the shipping state based on the chosen country during the checkout process.
  • if (is_checkout())  condition checks if the current page is the WooCommerce checkout page. If it is not, the code inside the function will not run.
  • Inside the JavaScript code (between <script> tags), the code does the following:
    • Listens for the ‘country_to_state_changed’ event. This event is triggered when the customer changes the selected country during the checkout process.
    • Within the event handler, it defines a function called ts_set_shipping_state() which will be used to set the selected shipping state.
    • It retrieves the selected country using $(‘#shipping_country’).val()  and stores it in the variable selectedCountry.
    • It then uses a switch statement to check the value of selectedCountry. In this example, it’s set up to handle the case when the selected country is ‘US’ (United States) and assigns ‘NY’ (New York) as the newState.
    • Finally, it sets the selected state in the shipping address form by setting the value of the shipping state dropdown ($(‘#shipping_state’)) to newState, and triggers a change event to ensure that WooCommerce updates the form accordingly.

Conclusion

The code provided above helps you to allow shipping to only one state. You can modify the code to restrict product availability by state or even to allow multiple states instead of a single state.

Let us know your feedback on how the code was useful or any other queries in the comments section.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
4 Comments
Newest
Oldest
Inline Feedbacks
View all comments
Temitayo
8 days ago

I want to limit only the states under shipping but this code also limits the billing state to New York. Any way out?

2 months ago

Thanks. I tried this code via Code Snippets and it doesn’t work for me. I get this error: Could not create snippet. Request failed with status code 403

4
0
Would love your thoughts, please comment.x
()
x