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 );
@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