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

18 replies
  1. Adeel
    Adeel says:

    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

    Reply
    • Nicola Mustone
      Nicola Mustone says:

      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.

      Reply
    • Nicola Mustone
      Nicola Mustone says:

      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.

      Reply
    • Nicola Mustone
      Nicola Mustone says:

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

      Reply
  2. mai
    mai says:

    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

    Reply
  3. Justin Pratscher
    Justin Pratscher says:

    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!

    Reply
  4. ryan
    ryan says:

    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

    Reply
  5. Vytis
    Vytis says:

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

    Reply
  6. Sam
    Sam says:

    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.

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

You have to agree to the comment policy.

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