WooCommerce - add City and District for shipping zones

How to add City and District for shipping zones in WooCommerce

Davor 03/07/2020

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

Then navigate to the shippings settings tab and add 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.

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.

Davor Veselinović

I'm Davor, founder of Worda Themes. Working as a Full Stack Developer with more than a decade of experience with WordPress. I build themes and plugins and enjoy writing useful blog posts and share my ideas with others.

Leave a Reply

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