One of the most powerful plugins for WooCommerce is Table Rate Shipping because it's so configurable. You can create your own rules for bulk shipping, weight based shipping, or set up shipping zones and charge different rates for each zone. When someone requests their existing shipping method only support certain zones, or charge extra for specific zones we usually point them to Table Rate Shipping because that extension can do just about anything.
It would be nice if we could take some of the functionality of that plugin out and apply it to other shipping methods (like UPS, FedEx, USPS, etc.) but at WooThemes we're very aware of feature creep and if we added these other feature the existing shipping methods would be bloated to the point of being unusable. So without adding a whole bunch of options that most users don't want the best way to do this is to write a snippet of code to do this.
Only Ship to Continental United States
I decided to dig in and create a mini plugin for users to which checks which state they are in and then disables all shipping methods if the user isn't in the continental United States. I'm including the plugin for you to look at the code but you can also go the plugins home page and download it, extract the zip file, and upload the folder to your /wp-content/plugins/
directory of your site.
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 ); |
Shipping Hooks
I've been doing support for WooCommerce for just about 6 months now and it's still amazing to me how easy it is to change something using the giant list of hooks we have. For shipping methods especially there's the woocommerce_available_shipping_methods
filter you can use to do modify them however you need.
Hopefully this plugin solves whatever needs you have and if not use that woocommerce_available_shipping_methods
hook andΒ happy coding! π
It looks like version: 2.0.20 changed the way shipping works?
Does this code work with lastest version of Woocommerce?
Hi Michelle! I just tested this myself and it seems to work fine. There’s most likely another plugin doing something to mess this up. Disable all other plugins and see if it works with flat rate shipping. Then add the plugins back in one by one.
Is there be a way to set this up for just a category? for example a client of mine sells a product that is broken down into 6 different categories of which one contains alcohol and can only be shipped in the state the company resides in. The rest of the products are available to be shipped to all the states except for those in the one category.
Hi Steven,
This shouldn’t be too hard but it would be a different snippet. I’d reach out to Codeable to customize this snippet. It should be a pretty quick customization so it shouldn’t cost an arm and a leg.
Hi Patrick,
your code is good but how to define any product in array.
I need to block certain product by id in specific states only. But shipping method will work which is defined at backend. I just ned to condition only area.Please help me out. Thanks in advance.
Steven,
I did a little poking around on the web, and came up with this modification: https://gist.github.com/anonymous/c947e54622d208939c72
In the code, the number ’33’ is the category ID that uses the “ship to” restriction, and as you’ll see in the excluded $states_array, I can only ship items in that category to two states, NH and MA.
Many thanks to Patrick’s excellent plugin for getting this started!
Thank you sir! Just what I needed.
Thank you, that’s exactly what I needed π Cheers!
Thanks for the info! Do you have any advice on how to restrict coupons to a specific country in Woocommerce?
Unfortunately that would have to be an entirely different snippet. I would look into the
woocommerce_coupon_is_valid
hook.What about only accepting orders from a specific state? Is that do-able?
This would have to be an entirely different snippet. You’d want to look into the
woocommerce_countries_allowed_country_states
hook.Hi Patrick,
This looks like an AWESOME plugin! It’s exactly what I’ve been looking for.
Unfortunately, I downloaded, installed, and activated it yet it doesn’t seem to be working. Maybe the WooCommerce 2.1 update broke it? Is there any way you know to get it running again?
Thanks!
I just tested it on my own test site and it works fine. I did replace the old filter,
woocommerce_available_shipping_methods
, with the new one,woocommerce_package_rates
. That should only have printed a warning to the screen if WP_DEBUG was active and not caused any sort of error. Regardless, give the new one a try. If that doesn’t fix it there’s got to be some sort of conflict with a theme or plugin on your site.Thanks for the post! Table Rate Shipping doesn’t work at all with UPS, correct? I have an ecommerce site selling wine, so we are restricted from shipping to certain states (in the U.S.A.). I’d think Table Rate Shipping would be the best option for limiting certain states, but after using it, it seems to override UPS. Furthermore, I can’t even figure how to restrict shipping to certain states using TRS. Am I missing something?
Thanks again!
You can use Table Rate Shipping to create shipping zones (where you may exclude certain states) but you have to create your own shipping methods for those shipping zones. You can’t use UPS shipping within Table Rate Shipping. You’d have to estimate UPS rates and set up your own rates.
This really isn’t the right medium for this. For questions like this I highly recommend reaching out to WooThemes support (who knows I might even get the ticket!) and they can give you a much more in-depth answer.
So, Patrick, does this mean your plugin can’t be used with http://www.woothemes.com/products/ups-shipping-method/ ? My client is shipping via UPS and trying to restrict one shipping method (Ground Shipping) to California (or better yet only a certain UPS shipping zone or zones). I’m still trying to figure out what cart to use. I’ve been building her a WordPress site using Enfold.
This snippet should work for any shipping method. π
For disabling a particular UPS method (but allowing other methods) will be trickier. I’d reach out to Codeable for a custom snippet.
Thanks, Patrick. I ended up not needing this, but for anyone else who might be searching for something similar, I found this guy http://bryanheadrick.com/product/woocommerce-ups-toolbox-print-ups-labels/ who was super helpful and responsive and was ready to do the custom work for me. Thanks again.
Thank you, is there a solution for excluding Alaska & Hawaii when using Table Rate Shipping?
If you’re using Table Rate Shipping you can create a shipping zone of the remaining 48 states without any code.
I have done this, but it isn’t eliminating Alaska, Hawaii, the Armed Force Options and Guam from the locations to ship to on the drop down.
Any other suggestions?
This plugin won’t remove those options from the dropdown. That’s not a good user experience since the user will keep searching for it and get frustrated. It’s better to show them the option and then tell them there aren’t any shipping methods for that area. If you want to remove the options from the dropdown you’ll have to write a very different plugin.
Hi Patrick,
I can’t manage to get your plugin to work. I tried disabling all other plugins, but no luck.
Not sure if you’re familiar with it, but I’m using the Avada theme (http://theme-fusion.com/avada/). They don’t have any instances of “woocommerce_available_shipping_methods”, “woocommerce_package_rates” in their theme.
Any idea on what the problem is? Any help is much appreciated!
Hey Brandon,
Either version of the plugin should work. The newest version, the one with
woocommerce_package_rates
, will make sure there are no warnings printed to the screen.Have you tried disabling the theme to see if that could be the cause?
Hey Patrick,
I tried changing the theme, but it still doesn’t work for me even with default theme Twenty Fourteen v1.1. I don’t see any errors on the screen either way. I still see Alaska, Hawaii, Guam, etc. though.
Thanks for responding so quickly. Hoping this clears things up. Please advise. Thanks in advance!
Hey Brandon – I edited your comment a bit for brevity. The issue here is that this plugin doesn’t remove those options from the dropdown. If someone selects that location they will get a message saying no shipping options are available. See this comment for why that’s a good idea.
I see. That makes sense. The only issue now is the error message given to the user. All it says is “Invalid shipping method”. Is there a way to customize the message if they do select a state that’s been removed?
We’re going a bit off topic here – try the gettext filter.
This is a nice catch-all, though I’m curious as to whether you’ve made it work per-product – if it could be an ‘on/off’, or ‘restrict shipping on this product’ or some such thing.
You can certainly extend this so it only applies to certain products. You’d want to add some extra conditional programming to the existing if statement. But depending on how you want this to work you may want to use Table Rate Shipping with shipping classes which is probably easier than writing the code.
Great, easy to use plugin. Thank you!
Hi!
I’m wondering if there’s a way to restrict shipping of a category of products to one specific state. We run a liquor store, and while wine can be shipped to many different states, hard liquor can only be shipped within NY. Is there a way to restrict just that category of products?
Thanks!
Yeah that’s certainly possible but it would be an entirely different filter. You’d have to add some extra conditionals in there. For something like that I would reach out to an affiliated WooWorker: http://www.woothemes.com/affiliated-woo-workers/
All I would need to add is one additional conditional for a certain attribute or category of products. Could you show me how to do that? I can create the additional filter, I’m just not sure how to add an additional conditional onto it.
Thanks so much!
Hey Patrick,
I am wondering how hard it would be to make this so that when it returns false the Payment Options and Place Order button disappear. I just wonder how many users will actually notice that they’re not purchasing shipping when they still have the option to check out…
I found an alternative that works if you only want to ship everything to the continental US, and just don’t want the other options listed as an option. Since it may work for others in a similar situation here it is (credit to http://www.terrytsang.com/tutorial/woocommerce/add-modify-states-for-woocommerce-checkout-form/)
I installed your plugin, but it does not seem to be working. Our store is http://www.maineloghome.com
Never mind. It requires a full address to be entered to block the shipping method.
Thanks for a great plugin.
Glad you got it to work! π
Is there a way to assign this to work with only some shipping methods? Such as only restrict Flat Rate Shipping to the Lower 48, and all other shipping methods are allowed.
Thanks!
A developer should definitely be able to do that. They’d probably only need to add a couple lines to my snippet. Codeable (aff) is my favorite service.
Nice work Patrick, One of my client want to exclude certain U.S. states from the shopping cart, but want to keep all others. Will this work ?
Yep this should definitely work! π
Hi Patrick,
Thanks for this great plugin. It almost does exactly what I need only I need to restrict certain UK postcodes, much like in the Local Delivery but for the Free Shipping rate.
Any ideas as to how I could achieve that?
Thanks
Hi Helen,
The code you would need would be similar to the code I have posted but obviously instead of states you’d want to get the postcodes. I’d reach out to Codeable to customize this snippet for you. They excel at these small jobs.
Hi, I downloaded your plugin in hopes that it would allow me to set up shipping to US states… Do you know of a function that would allow me to choose states in the WC commerce settings area? Seems simple enough but I have been looking through lines of code trying to chase it.. If you have any info I would appreciate it!
Basically you want to be able to select exactly which states have certain types of shipping? I’d actually checkout last weeks post about WooCommerce Table Rate Shipping. The zones are incredibly good at what you’re trying to setup. It’s expensive but worth it since it saves you so much time and it’s so configurable.
How can I make the github plugin only apply to certain shipping classes?
Hey Tim,
That would have to be more customized. I’d reach out to Codeable for this. They specialize in quick customizations.
Any chance this can be modified so that WooCommerce does not ship to my home state of Washington?
Or can I do that manually somehow with WooCommerce without your plugin? Thanks!
Hi Andy. I’d actually use the plugin. Just change the
$excluded_states
array to include only Washington (WA).Call me an idiot, but I don’t see where I can download the plugin on your page? Please help. π
Hi Kaje!
First, you’re definitely not an idiot.
If you scroll up you’ll see a link that says
plugins home page
. Click on that link. From there you should see a button sayingDownload Gist
. Download it and then extract the zip file and upload it to your site via FTP.I hope that helps! π
Hello β awesome! this plug-in seems to have resolved a big shipping problem that we have been having. Just one question: Is it possible to customize the error message that customers receive? It is: “There doesnβt seem to be any available shipping methods. Please double check your address, or contact us if you need any help.”
Hey Mike that’s definitely possible. Have you seen my post on the gettext filter?
Hello Patrick, Nice article. Have been working on shipping plugins and you have also inspired us to write our own plugins. Thanks! Just thought of suggesting this simple plugin (free to download from wordpress plugin repository) which helps in configuring weight based shipping rates. Details available here http://www.wooforce.com/how-weight-country-woocommerce-shipping-plugin-enhanced-simplified-woocommerce-shipping-module/
This was an attempt to simplify the weight based plugin. Download link https://wordpress.org/plugins/weight-country-woocommerce-shipping/ .
Any inputs are appreciated!
Cheers!
The plugin looks really nice. Thanks for sharing. π
Patrick, great help as I am working on a plugin to ship only to specific zip codes. π
Awesome!
I have to hide payment option if Alaska or Hawaii state is chosen & also display message to user for contacting admin if none of above (Alaska & Hawaii) state is chosen than show free shipping.
thank you for the plugin – works!
Not working in 2.4.10 Have checked and doubled heck plug-in conflicts, no joy.
Is there an update to this plug-in? I want to use it but doesn’t seem to be working for me in 2022