Menu Close

WooCommerce: Limit Shipping to Only One State

default

Today’s snippet has been widely requested by many readers, clients and WooCommerce fans. We already saw in the past how to Limit State Dropdowns to One State Only (for both Shipping & Billing) and How to Sell to one State only (Billing).

However, we never covered a much more common setting: what happens when Billing is allowed to every state but Shipping is limited?

In order to get a little help, I’ve reached out to Diego Zanella, a WooCommerce genius who is also the author of the Aelia Currency Switcher plugin for WooCommerce.

PHP Snippet (1 of 2): Allow Shipping to Only One State @ WooCommerce Cart Shipping Calculator

/**
 * @snippet       Ship to One State @ WooCommerce Cart Shipping Calculator
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Diego Zanella
 * @compatible    WooCommerce 3.6.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_filter( 'woocommerce_states', 'aelia_cart_filter_US_states' );
 
function aelia_cart_filter_US_states( $states ) {
if ( is_cart() ) {
   $states['US'] = array(
      'PA' => 'Pennsylvania',
   );
}
return $states;
}
WooCommerce: Allow Shipping to Only One State in the Cart Shipping Calculator
WooCommerce: Allow Shipping to Only One State in the Cart Shipping Calculator

PHP Snippet (2 of 2): Allow Shipping to Only One State @ WooCommerce Checkout Page

Note: this will only work if you’ve limited shipping to a specific country via the WooCommerce settings. Also, this snippet might limit billing too – in this case check the alternative snippet below.

/**
 * @snippet       Ship to One State @ WooCommerce Checkout #1
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.6.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

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

Alternative snippet – in case everything else fails:

/**
 * @snippet       Ship to One State @ WooCommerce Checkout #2
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.6.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'wp_footer', 'aelia_checkout_shipping_filter_US_states' );
  
function aelia_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
}
WooCommerce: Allow Shipping to Only One State @ Checkout
WooCommerce: Allow Shipping to Only One State @ Checkout
View Source
Posted in WooCommerce Tips