Custom states and cities in both cart and checkout page at WooCommerce as dropdown selection

Is there anyway to get list of custom states and cities in both cart and checkout page at WooCommerce? We want to select state among the available list of states and then select associated cities of that particular state. For example: If state 1 is selected then city 1 and city 2 appears and so on appears at the dropdown. If state 2 is selected the city 3 and city 4 and so appears at the dropdown. Hoping for response.

Thank you and best regards.

Hi @bibhashkarn,

Thanks for reaching out. This isn’t possible with WooCommerce natively, but might be possible through custom development. WooCommerce has hooks and filters for modifying the states that appear: https://docs.woocommerce.com/document/addmodify-states/

It is also possible to modify the type of a field with the woocommerce_checkout_fields hook. Here’s an example I found of making the city a select field: https://gist.github.com/JeroenSormani/564f66bd2f697291e6c37b1ed3c595dc#file-woocommerce-change-city-field-to-dropdown-php

Neither of these solve the issue of making the city options populate based on the selected state. This would likely take some custom javascript to do something like this:

  • Watch for changes to the state
  • Query the server with AJAX to get a list of cities in that state
  • Update the options of the city select with the new cities.

All of this is well outside the scope of theme support, so regretfully we won’t be able to assist further. Hopefully this gives you some ideas on how to get started if you want to code your own solution, or it may help shape a discussion if you bring it to a developer.

Hi,

Thanks for reply and useful documentation. Thanks for the links. We will work for this.

Is there any option to get list of cities that you have mentioned via “woocommerce_checkout_fields” in the cart page too. We are able to get list of cities only at checkout page but we are looking cities list in cart page also.

Thank you and best regards.

Hi Bibhash,

There is no simple hook from what I know. You will need some customization to be able to add extra fields such as cities on the cart page. Kindly check this and this article for more information.

I also performed a Google Search that might help you to get started with your research.

Unfortunately, we will not be able to help you further as the request is outside of our support scope and not related to our product.

Thank you for your understanding.

Hello,

Thank you for the link and google search. We will work for the customization.

Thank you and best regards.

You’re most welcome!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.