Only Ship to Continental United States with WooCommerce

Continental United States
  1. Blogging for Benjamin Competition
  2. Why I'm Grateful to Work on the Web
  3. 24 Pull Requests
  4. Update Downloadable Product's Expiration Date in WooCommere
  5. Get Lost in the Flow and Work for More Than a Salary
  6. Why A Plugin's Popularity Matters
  7. Why You Should (Or Shouldn't) Use Premium Plugins
  8. WooCommerce Terms & Conditions
  9. Only Ship to Continental United States with WooCommerce
  10. Just Talk
  11. Why I Love Jetpack
  12. Making Jetpack Better
  13. Remove Billing Address for Free Virtual Orders in WooCommerce
  14. Notify Admin of Customer Address Change in WooCommerce
  15. Open Your Self Up To New Possibilities
  16. 2013 Resolutions Review
  17. Create a Community
  18. Tips for Starting a Community
  19. The Intent of Goals
  20. Create The Ultimate Invoicing System Using WooCommerce
  21. Change From Address in Ninja Forms
  22. Work With People Who Inspire You
  23. Contact Form 7 & MailPoet Integration
  24. Monotasking
  25. Giving Back to The Community
  26. Adding Fuctionality to Lean Plugins
  27. Choose Stripe For a Payment Gateway
  28. A Dip Into Entrepreneurship
  29. Reward Yourself
  30. Blogging for Benjamin Plugin
  31. Blogging for Benjamin Wrap Up

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.


<?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! πŸ™‚

64 thoughts on “Only Ship to Continental United States with WooCommerce

  1. 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!

  2. Thank you sir! Just what I needed.

  3. Thank you, that’s exactly what I needed πŸ™‚ Cheers!

  4. Thanks for the info! Do you have any advice on how to restrict coupons to a specific country in Woocommerce?

  5. What about only accepting orders from a specific state? Is that do-able?

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. 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.

  11. Great, easy to use plugin. Thank you!

  12. 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!

  13. 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…

  14. 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/)

  15. I installed your plugin, but it does not seem to be working. Our store is http://www.maineloghome.com

  16. 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!

  17. 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 ?

  18. 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.

  19. 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!

  20. How can I make the github plugin only apply to certain shipping classes?

  21. 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!

  22. 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 saying Download Gist. Download it and then extract the zip file and upload it to your site via FTP.

      I hope that helps! πŸ™‚

  23. 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.”

  24. 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!

  25. Patrick, great help as I am working on a plugin to ship only to specific zip codes. πŸ˜‰

  26. 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.

  27. thank you for the plugin – works!

  28. Not working in 2.4.10 Have checked and doubled heck plug-in conflicts, no joy.

  29. Is there an update to this plug-in? I want to use it but doesn’t seem to be working for me in 2022

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.