Sell in specific States/Provinces/Regions with WooCommerce

As you know, WooCommerce allows you to sell only in specific countries, like only in Italy or in Unites States.
What if you want to sell only in a specific province, or only in California for example?

There’s a little snippet of code to add to your site. In this example i want to sell only in California so i need to update the states dropdown in the checkout page to include only that state.

To do so, open your functions.php file located in wp-content/themes/your-theme-name/ and add this code at the end of the file:

Delete the <?php first line if you are having errors come up after saving the file.*

Done! Now you will see only California in the states dropdown on your checkout page.

If you need a list of all the states/regions/provinces/counties available in WooCommerce, you can find them in wp-content/plugins/woocommerce/i18n/states/.


More Posts That You Might Like…


20 responses to “Sell in specific States/Provinces/Regions with WooCommerce”

  1. Hi Nicola ,

    Hope you are doing well .

    I have read you post which is very helpful for me to sell the products in specific region but now i want to sell the products through the zip codes and only allow the desire zip code to purchase the product rest of the zip codes are restricted to buy. Also i want to set the different pricing for the specific zip codes.

    Please suggest me any extension if available their OR do some code amendments to achieve the task.

    I will rewards you the bundle of coffee dont worry about that bro 🙂 .

    Thank you very much !

    Best regards,
    Adeel

    1. Hi Adeel,
      i would suggest to use Table Rate Shipping: https://www.woothemes.com/products/table-rate-shipping/

      With it you can specify shipping rules based on the zip code. If the customer’s zip code is not allowed, he will not be able to purchase because there won’t be a shipping method available.

      For the prices based on the zip code, i don’t think there’s a plugin for this, and also i don’t think it could work properly.
      The zip code is only checked on the checkout page, so you should change the prices there. Will the customer see this change on the checkout page?

      I don’t think so, and you will receive a lot of messages about it.
      Anyway, i’m sorry but i don’t have a code or a plugin for this.

  2. Hi sir,
    Please,
    How can i use your code to sell a products in a one specific country ? Please

    Best regards
    A.J.

  3. Hi,
    you don’t need my code to sell in a specific country. You can limit the countries where you sell from the settings in WooCommerce > Settings > General!

  4. Can you provide a screenshot? -Newbie.

    1. Sorry but a screenshot of what? It does not change anything visible. You will still see the default WooCommerce checkout page, only the states dropdown will have less options to choose from.

  5. How would I adjust the code for Canadian Provinces?

    1. You need to change $states['US'] to $states['CA'] and then list all the provinces you want to show. You can get the list of Canada’s provinces from the file CA.php located in wp-content/plugins/woocommerce/i18n/states/.

  6. Hello
    Thank u for the code

    Please what if I have some products that can only be deleivered localy, but we sell other products all over the world.
    What is a way to restrict these products to ONE contry only , not restric all my store and its products (countries settings in woocomerce general)

    for example, flower and chocolate, i only want to sell it locally

    but i want that before the checkout page, as it will take customer time to write everything and then he sees he cnat order

    like amazon tells u they cant ship to this address in the very last step

    thank u
    waiting ur replay

  7. Justin Pratscher Avatar
    Justin Pratscher

    Hi. This works great, but I was wondering if you know of a solution that would allow me to adjust only the shipping address states, leaving the billing address states in tact.

    Thanks!

    1. Hi Justin,
      I didn’t test this personally, but try to change woocommerce_states with woocommerce_countries_shipping_country_states on line 12 of the snippet above.

      1. I was looking for restricting only shipping address too. That didn’t work for me. If you have other ideas…

        1. Same here. Don’t want to restrict Billing

  8. Hi, Nicola great code I’m getting errors when adding more than one state. I’m not closing or opening at the right time between parts of the array? Could you add a snippet of more than one state for syntax clarity?
    thanks

    1. Hi Ryan,
      You can find here an example with multiple states: https://gist.github.com/SiR-DanieL/d297db06c39a9531e408ad8a6fde44ec

  9. Great code, but is there any way to restrict based on shipping class or category? Your code is nearly perfect for my needs.

  10. Thank you very much it worked very well. I sell in australia, victoria only.

  11. Restrict based on shipping and not both billing and shipping?

    Say I restrict shipping to Alaska. I still want someone from Alaska to be able to buy it and ship to a state that’s not restricted.

  12. You saved my day thank you!!!!

  13. Hey Nicola! Great code. thanks for sharing. There is one thing I wonder if you have a solution. I have a product which I am selling to Dublin, Ireland. But within Dublin, for some zones like D1 to D10 I want that the shipping is free shipping and for other zones there will be shipping cost.

    I hope my question is clear to you. If you could help me out, it will be a great pleasure for me.

    Thanks

Leave a Reply

Categories

Newsletter

Receive new articles from this blog directly in your inbox!

No spam guaranteed!

Blog at WordPress.com.

%d bloggers like this: