Skip to content

Instantly share code, notes, and snippets.

@BFTrick
Last active December 7, 2023 08:47
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save BFTrick/7805588 to your computer and use it in GitHub Desktop.
Save BFTrick/7805588 to your computer and use it in GitHub Desktop.
WooCommerce only ship to the continental US.
<?php
/**
* Plugin Name: WooCommerce Only Ship to Continental US
* Plugin URI: https://gist.github.com/BFTrick/7805588
* Description: Only Ship to the Continental US
* Author: Patrick Rauland
* Author URI: http://patrickrauland.com/
* Version: 1.0.1
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Patrick Rauland
* @since 1.0.1
*/
/**
* Only ship to the continental US
*
* @param array $available_methods
*/
function patricks_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', 'patricks_only_ship_to_continental_us', 10 );
@BFTrick
Copy link
Author

BFTrick commented Jan 21, 2015

Should definitely be compatible with WC 2.2.

@szyam
Copy link

szyam commented Feb 5, 2015

hmmm, i could be doing something wrong but doesn't seem to be working as of 2.2.11. Tried inserting into functions, and then installed as plugin. The above states are still present in the select when checking out...

@InteractM
Copy link

Confirmed - not working with 2.2.11

@jleandro
Copy link

jleandro commented Nov 4, 2015

It simply needed a small update... I confirm the code below is working with WC 2.4.8:

function patricks_only_ship_to_continental_us( $available_methods ) {

$excluded_states = array( 'PM','CE','GC','ML','TF' );

if( in_array( WC()->customer->get_shipping_state(), $excluded_states ) ) {
    // Empty the $available_methods array
    $available_methods = array();
}

return $available_methods;

}
add_filter( 'woocommerce_package_rates', 'patricks_only_ship_to_continental_us', 10 );

@greguly
Copy link

greguly commented Dec 8, 2015

A slightly different approach (from code perspective) is

// remove free shipping if not in continental US
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'greguly_wc_free_shipping_only_to_continental_us' );

/**
 * Free Shipping available only for continental US
 */

function greguly_wc_free_shipping_only_to_continental_us( $is_available ) {
    $excluded_states = array( 'AK', 'HI', 'GU', 'PR', 'AA', 'AE','AP', 'AS', 'GU', 'MP', 'PR', 'UM', 'VI' );
    if ( in_array( WC()->customer->get_shipping_state(), $excluded_states ) ) {
        $is_available = false;
    }
    return $is_available;
}

Tested with WC 2.4.11

Enjoy :-)

@ltgraphix
Copy link

I know this is what I am looking for but when adding to the wp-content/plugins directory, I am still not seeing this option anywhere and it still allows users to purchase outside of continental us. I am also using table-rates for certain zones within the US. Not sure what I am doing wrong here. :/

@ekazda
Copy link

ekazda commented Apr 27, 2016

This doesn't appear to do anything for Woocommerce 2.5.5.

@itskaeon
Copy link

Woocommerce 2.6.4 it will not show other shipping method just says there is no shipping options. Any fix?

@lukecav
Copy link

lukecav commented Nov 8, 2017

`add_filter( 'woocommerce_states', 'custom_woocommerce_states' );

function custom_woocommerce_states( $states ) {
$excluded_states = array('AK', 'AA', 'AE', 'AP', 'AS', 'GU', 'MP', 'PR', 'UM', 'VI', 'HI');
foreach($excluded_states as $no_state){
unset($states['US'][$no_state]);
}

return $states;
}`

Works fine in WC 3.2.3

@Peregrine-Design
Copy link

None of these functions work with WP 4.9.1 and WC 3.2.x. lukecav, your function works but it is for all '$state' lists and the goal is to exclude only states for shipping. I'm waiting on a fix to go live with a site. Any help would be great! Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment