The following will change billing and shipping city fields to a dropdown on
The code:
// Custom function that handle city options
function get_city_options() {
$options = array(
// '' => __( 'Select unit type' ), /*= this will make empty field*/
'Jeddah' => __('Jeddah', 'custom-domain'),
);
ksort($options);
return $options;
}
// Checkout and my account (edit) billing and shipping city
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_city_fields' );
function custom_override_default_city_fields( $fields ) {
$fields['city']['type'] = 'select';
$fields['city']['class'] = array('my-field-class form-row-last');
$fields['city']['input_class'] = array('state_select');
$fields['city']['options'] = get_city_options();
$fields['city']['priority'] = '41';
return $fields;
}
// Admin editable single orders billing and shipping city field
add_filter('woocommerce_admin_billing_fields', 'admin_order_pages_city_fields');
add_filter('woocommerce_admin_shipping_fields', 'admin_order_pages_city_fields');
function admin_order_pages_city_fields( $fields ) {
$fields['city']['type'] = 'select';
$fields['city']['options'] = get_city_options();
$fields['city']['class'] = 'short'; // Or 'js_field-country select short' to enable selectWoo (select2).
return $fields;
}
// Admin editable User billing and shipping city
add_filter( 'woocommerce_customer_meta_fields', 'custom_override_user_city_fields' );
function custom_override_user_city_fields( $fields ) {
$fields['billing']['fields']['billing_city']['type'] = $fields['shipping']['fields']['shipping_city']['type'] = 'select';
$fields['billing']['fields']['billing_city']['options'] = $fields['shipping']['fields']['shipping_city']['options'] = get_city_options();
return $fields;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Addition - Other billing and shipping fields
For other billing and shipping fields than WooCommerce default addresses fields you can replace:
// Checkout and my account (edit) billing and shipping city
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_city_fields' );
function custom_override_default_city_fields( $fields ) {
// … …
return $fields;
}
by the 2 following hooked functions:
// For billing custom field (Checkout and my account edit addresses):
add_filter( 'woocommerce_billing_fields' , 'custom_override_billing_fields' );
function custom_override_billing_fields( $fields ) {
$fields['billing_custom1'] = array(
'type' => 'text', // chose the field type
'label' => __('Custom label (billing)', 'woocommerce' ),
'class' => array('form-row-wide'), // can be also 'form-row-first' or 'form-row-last'
'required' => false, // Optional
'clear' => true, // Optional
'priority' => 200, // To change the field location increase or decrease this value
);
return $fields;
}
// For shipping custom field (Checkout and my account edit addresses):
add_filter( 'woocommerce_shipping_fields' , 'custom_override_shipping_fields' );
function custom_override_shipping_fields( $fields ) {
$fields['shipping_custom1'] = array(
'type' => 'text', // chose the field type
'label' => __('Custom label (shipping)', 'woocommerce' ),
'class' => array('form-row-wide'), // can be also 'form-row-first' or 'form-row-last'
'required' => false, // Optional
'clear' => true, // Optional
'priority' => 200, // To change the field location increase or decrease this value
);
return $fields;
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.