Last active
December 7, 2023 08:47
-
-
Save BFTrick/7805588 to your computer and use it in GitHub Desktop.
WooCommerce only ship to the continental US.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
`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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Woocommerce 2.6.4 it will not show other shipping method just says there is no shipping options. Any fix?