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 tutoraspire.com FREE
* @author Diego Zanella
* @compatible WooCommerce 3.6.4
* @donate $9 https://tutoraspire.com
*/
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;
}
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 tutoraspire.com FREE
* @author Tutor Aspire
* @compatible WooCommerce 3.6.4
* @donate $9 https://tutoraspire.com
*/
add_filter( 'woocommerce_countries_shipping_country_states', 'tutoraspire_set_checkout_shipping_state' );
function tutoraspire_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 tutoraspire.com FREE
* @author Tutor Aspire
* @compatible WooCommerce 3.6.4
* @donate $9 https://tutoraspire.com
*/
add_action( 'wp_footer', 'aelia_checkout_shipping_filter_US_states' );
function aelia_checkout_shipping_filter_US_states() {
if ( ! is_checkout() ) {
return;
}
?>