-
-
Save BFTrick/7805588 to your computer and use it in GitHub Desktop.
<?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 ); |
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 :-)
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. :/
This doesn't appear to do anything for Woocommerce 2.5.5.
Woocommerce 2.6.4 it will not show other shipping method just says there is no shipping options. Any fix?
`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
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.
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 ) {
}
add_filter( 'woocommerce_package_rates', 'patricks_only_ship_to_continental_us', 10 );