Ecommerce Shopify WordPress Discussion

Select Region Then City in WooCommerce checkout

I am trying to display custom checkout fields in WooCommerce. Our country is divided into regions. What I want is to choose the region first, then the city. The code I'm using shows the region dropdown field but doesn't show the city select field: function custom_override_checkout_fields( $fields ) { $fields['billing']['billing_region'] = array( 'type' => 'select', 'options' => array( 'guria' => 'Guria', 'adjara' => 'Adjara', // Add other regions here ), 'required' => true, 'label' => __('Region', 'woocommerce') ); $fields['billing']['billing_city'] = array( 'type' => 'select', 'options' => array(), 'required' => true, 'label' => __('City', 'woocommerce') ); return $fields; } add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' ); function load_custom_js() { wp_enqueue_script( 'custom-js', get_template_directory_uri() . '/custom.js', array( 'jquery' ), '', true ); wp_localize_script( 'custom-js', 'regions_cities', array( 'guria' => array('City1', 'City2'), 'adjara' => array('City3', 'City4'), // Add other regions and their cities here )); } add_action( 'wp_enqueue_scripts', 'load_custom_js' ); I also use the js code which is in the correct directory. The console log is clean and does not show any errors. jQuery(document).ready(function($) { $('select#billing_region').change(function() { var selectedRegion = $(this).val(); var cities = regions_cities[selectedRegion]; var citySelect = $('select#billing_city'); citySelect.empty(); $.each(cities, function(index, city) { citySelect.append($('<option></option>').attr('value', city).text(city)); }); }); });
There are some mistakes and missing things in your code… Try the following revised code: function get_regions(){ return array( 'guria' => 'Guria', 'adjara' => 'Adjara', // Add other regions here ); } function get_regions_cities(){ return array( 'guria' => array( 'city1' => 'City 1', 'city2' => 'City 2' ), 'adjara' => array( 'city3' => 'City 3', 'city4' => 'City 4' ), ); } add_filter( 'woocommerce_checkout_fields' , 'regions_cities_custom_checkout_fields' ); function regions_cities_custom_checkout_fields( $fields ) { $fields['billing']['billing_region'] = array( 'type' => 'select', 'options' => get_regions(), 'required' => true, 'label' => __('Region', 'woocommerce') ); $fields['billing']['billing_city']['type'] = 'select'; $fields['billing']['billing_city']['options'] = array('' => ''); $fields['billing']['billing_city']['required'] = true; return $fields; } add_action( 'wp_enqueue_scripts', 'enqueue_regions_cities_scripts' ); function enqueue_regions_cities_scripts() { // Note: for child themes use: get_stylesheet_directory_uri() wp_enqueue_script( 'regions-cities-js', get_template_directory_uri() . '/regions-cities.js', array( 'jquery' ), '', true ); wp_localize_script( 'regions-cities-js', 'wc_params_regions_cities', get_regions_cities() ); } Code goes in function.php file of your (child) theme or in a plugin. The JavaScript file regions-cities.js: jQuery( function($){ if (typeof wc_params_regions_cities === 'undefined') return false; function setCitiesOptionsForRegion( region ) { var cityOptions = wc_params_regions_cities[region], citiesField = $('select#billing_city'); citiesField.empty(); $.each(cityOptions, function(slug, name) { citiesField.append($('<option></option>').attr('value', slug).text(name)); }); } setCitiesOptionsForRegion( $('select#billing_region').val() ); $('select#billing_region').change(function() { setCitiesOptionsForRegion( $(this).val() ); }); }); Tested and works. Now when checkout loads, the Billing City dropdown field appear, and it's populated with the corresponding cities:

January 20, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.