Reputation: 580
I am trying to find a way to remove a few US states from the "State" dropdown in WooCommerce checkout page.
List of states:
What I have been doing so far:
I have been manually removing them as mentioned in this link. But this is not a good way, as the changes get overwritten every time WooCommerce is updated.
Alternative I found: On this link WooCommerce restrict shipping to states based on category, there's a way to set shipping states if specific condition is met. I am wondering if I can unset five states. This sounds more logical to me than setting 50 states. But unfortunately, I am not able to find anything useful.
Any ideas what could be a possible solution?
Upvotes: 2
Views: 10444
Reputation: 253773
The simplest way is to remove them them using the dedicated filter hook woocommerce_states
this way:
add_filter( 'woocommerce_states', 'custom_us_states', 10, 1 );
function custom_us_states( $states ) {
$non_allowed_us_states = array( 'AK', 'HI', 'AA', 'AE', 'AP');
// Loop through your non allowed us states and remove them
foreach( $non_allowed_us_states as $state_code ) {
if( isset($states['US'][$state_code]) )
unset( $states['US'][$state_code] );
}
return $states;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and work.
Then you can add some condition or make a custom settings page like in the threads below:
Upvotes: 13
Reputation: 659
Best way of doing this would be using a filter, add this snippet in your themes functions.php
/**
* Modify Woocommerce states array
*
* @param array $states, collection of all $states
* @return array $states, modified array.
*/
add_filter( 'woocommerce_states', function( $states ){
// Unset Hawaii
unset( $states['US']['HI'] );
return $states;
}, 999);
You can unset any states like this.
Upvotes: 2