Override shipping/billing city
The Phone Orders for WooCommerce plugin uses WooCommerce’s default city lists for billing and shipping addresses. In some workflows, you may need to replace these dynamic lists with a custom, fixed set of cities. This is particularly useful for stores that serve a specific region where the standard city names differ from the customer’s entered value, or when you want to enforce a limited selection. The code below overrides the billing and shipping city fields with a predefined, hard‑coded list.
The Complete Code Snippet
Visit “Settings” – “Custom fields” and add to section “Customer fields” following text (edit list for your needs)
|
1 |
City|city|select|CityName1|CityName2|CityName3|CityName4 |
Add the following PHP code to your child theme’s functions.php file or via the Code Snippets plugin.
|
1 2 3 4 5 6 7 8 9 10 |
add_action( "wpo_set_cart_customer", function ( $cart_customer, $id, $customer_data ) { /* @var WC_Customer $cart_customer */ if ( $cart_customer->meta_exists( 'city' ) ) { $city_code = $cart_customer->get_meta( 'city' ); $cart_customer->set_billing_city( $city_code ); $cart_customer->set_shipping_city( $city_code ); $cart_customer->apply_changes(); } }, 10, 3 ); |
Customisation Options
You can easily adapt the code to handle different scenarios.
Use a text field instead of a dropdown – Change the custom field definition to text:
|
1 |
textCity|city|text |
The agent can then type any city name.
Use a radio button group for a smaller set – Replace select with radio:
|
1 |
City|city|radio|New York|Los Angeles|Chicago |
Apply the override only to specific user roles – Add a condition based on the customer’s role:
|
1 |
$user = get_userdata( $id ); if ( in_array( 'wholesale_customer', (array) $user->roles ) ) { // apply override } |
Override only the billing city but keep the original shipping city – Remove the line $cart_customer->set_shipping_city( $city_code );.
Use a different meta key – Change city to any other key you define in the custom fields settings.