WooCommerce: Free Shipping For Continental U.S. Only

Based on code from Patrick Rauland, this bit of code will remove the WooCommerce free shipping option for users outside the continental United States. Simply place this code in a plugin or your functions.php file.

/**
* Only offer free shipping to the continental US
*
* @param array $available_methods
*/
function only_free_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 ) ) {

      if (array_key_exists ( 'free_shipping' , $available_methods )) {
         unset($available_methods['free_shipping']);
      }

   }

   return $available_methods;
}

add_filter( 'woocommerce_available_shipping_methods', 'only_free_ship_to_continental_us', 10 );