Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 );
@brnatermedia

This comment has been minimized.

Copy link

@brnatermedia brnatermedia commented Dec 1, 2014

@patrick is this compatible with WooCommerce 2.2??

@BFTrick

This comment has been minimized.

Copy link
Owner Author

@BFTrick BFTrick commented Jan 21, 2015

Should definitely be compatible with WC 2.2.

@szyam

This comment has been minimized.

Copy link

@szyam 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

This comment has been minimized.

Copy link

@InteractM InteractM commented Mar 2, 2015

Confirmed - not working with 2.2.11

@jleandro

This comment has been minimized.

Copy link

@jleandro 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

This comment has been minimized.

Copy link

@greguly 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

This comment has been minimized.

Copy link

@ltgraphix ltgraphix commented Jan 30, 2016

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

This comment has been minimized.

Copy link

@ekazda ekazda commented Apr 27, 2016

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

@itskaeon

This comment has been minimized.

Copy link

@itskaeon itskaeon commented Sep 15, 2016

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

@lukecav

This comment has been minimized.

Copy link

@lukecav 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

This comment has been minimized.

Copy link

@Peregrine-Design Peregrine-Design commented Dec 29, 2017

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