How to add City and District for shipping zones in WooCommerce

In some specific cases, you might need to change the WooCommerce default country, state or region to better suit your needs. For example, if you are creating a city-specific service that delivers the orders only inside the city boundaries.

Woocommerce comes with the predefined country list as well as the states and regions. However, this list is not so accurate as you might need to limit the shipping zone to the city districts or municipalities.

How to add custom country in WooCommerce

The first thing we need to do is to add the function that will register our country as a custom country inside the WooCommerce. To do so, locate the functions.php file located inside your child theme and paste this code inside:

add_filter( 'woocommerce_countries',  'worda_add_my_country' );
   function worda_add_my_country( $countries ) {
     	$new_countries = array(
                          'MYCT'  => __( 'My new country', 'woocommerce' ),
                          );

      return array_merge( $countries, $new_countries );
   }

add_filter( 'woocommerce_continents', 'worda_my_country_to_continents' );

   function worda_my_country_to_continents( $continents ) {
      $continents['EU']['countries'][] = 'NSNS';
      return $continents;
}

Replace the value ‘My new country’ with the name of the county you want to add and save changes. If you want, you can also replace the MYCT variable with your custom name, or change the “continent” to some other value than the EU. You can also reference the WooCommerce docs.

Now, you should be able to select your newly create country inside the WooCommerce general options.

How to add city and districts in WooCommerce

Woocommerce doesn’t come with the data that assembles all city districts around the world. To set up the shop that covers only one city and to charge different shipping costs for distant parts of the cities or another city zone, use this workaround.

In the same way, you’ve registered your custom country (like in the previous example) you could instead set the name of the city.

After that, paste the below code inside your functions.php file.

add_filter( 'woocommerce_states', 'worda_woocommerce_states' );
 
   function worda_woocommerce_states( $states ) {
    
       $states['MYCT'] = array(
           

               'MYCT1' => __('City Center', 'woocommerce') ,
               'MYCT2' => __('Michael Ave', 'woocommerce') ,
               'MYCT3' => __('Upper South Crec', 'woocommerce') ,
               'MYCT4' => __('Hepiorgont Park', 'woocommerce') ,
               'MYCT5' => __('Upper bosik', 'woocommerce') ,
               'MYCT6' => __('Downtown Jaic', 'woocommerce') ,
               'MYCT7' => __('Upper West Sopan', 'woocommerce') ,
               'MYCT8' => __('Poggub Hill', 'woocommerce') ,
               'MYCT9' => __('North Bob', 'woocommerce') ,
               'MYCT10' => __('Bayside Ford', 'woocommerce') ,

           //
           // You can add more...
           //


       );

       return $states;
}

After you add this code, modify the names for your future regions or city districts. Please also change the prefix variable MYCT if you have altered it to your own value. Save changes and check up the WooCommerce options.

You will be able to set the country to the registered city from the first code example.

Shipping and flat pricing for your custom city and district

Then navigate to the shippings settings tab and add a new Flat price, the city districts that have been registered via the second code example are now selectable from the Zone regions field. Mark the part of the city that you want to apply this new shipping rate based on the zone, then just create a new flat shipping rate for another zone and add the districts or city regions and apply different shipping prices for them.

IMPORTANT NOTE: According to recent changes in the Google Search algorithm, slow websites are being penalized by Google and it affects raking and position of your website on already established keywords.

To protect your website’s hard-earned positions, consider taking our SEO & Performance optimization service for a $100, one-time fee.

Conclusion

This simple hack should work out and it’s easy to apply. Once you register the city and districts you can also adjust the form label to represent the proper names, like city and regions. We also have an article on how to remove or fields from the checkout and ordering pages.

finally, if you stumble upon any issues, use the comments section to ask for assistance or share your impressions.

Share this post if you like it.

3 Comments

  1. Hi, this is a really useful post. But I do have problems and I hope you could help us out.

    After adding PHP function above. The only problem I encountered is that there is no ‘match selection’ in the woocommerce “zone region” under my Shipping Zone.

    Could you help us out?

  2. I did exactly as you stated, but it is not working. Nothing shows in the Zone and Region section. Is there anything else I need to do? Should I modify the files from the Woocommerce plugin rather?

Leave a Reply