Shipping to Alaska and Hawaii is very different from shipping to the rest of the continental United States. The rates and time to deliver are much higher. You may want to disable some or all shipping methods from shipping to those states. You could do this with Table Rate Shipping but if you don't want to purchase that extension you could write a bit of code that does the same thing.
Make sure shipping is enabled on your site. See the Setting a minimum order amount to unlock free shipping recipe in this chapter to see how this is done.
In order to only allow shipping to the continental US, let's go through the following steps:
functions.php
file, which is located at wp-content/themes/your-theme-name/
, or a custom WooCommerce plugin in your code editor. Paste the following code at the bottom of the file:function woocommerce_cookbook_only_ship_to_continental_us( $available_methods ) { global $woocommerce; $excluded_states = array( 'AK','HI','GU','PR' ); if ( in_array( $woocommerce->customer->get_shipping_state(), $excluded_states ) ) { // Empty the $available_methods array $available_methods = array(); } return $available_methods; } add_filter( 'woocommerce_package_rates', woocommerce_cookbook_only_ship_to_continental_us, 10 );
When WooCommerce is calculating rates, we add some extra logic to determine whether the shipping methods are valid. We create an array of states, and if the shipping address has one of the states in the array, then we return a list with no valid shipping methods. If the shipping address doesn't have one of the states mentioned, we do not do anything and return the list of states that was sent to this function.