Based on the code from the answer here Show sub Areas dropdown based on chosen city in WooCommerce checkout, I successfully made some changes in the code to fit my needs:
function cities_areas_settings() {
$text_domain = 'woocommerce';
return array(
__('', $text_domain) => array(
),
__('Test', $text_domain) => array(
__('Street 2', $text_domain),
__('Street 3', $text_domain),
__('Street 4', $text_domain),
),
__('Test1', $text_domain) => array(
__('Street 5', $text_domain),
__('Street 6', $text_domain),
),
__('Test2', $text_domain) => array(
__('Street 7', $text_domain),
__('Street 8', $text_domain),
),
__('Test3', $text_domain) => array(
__('Street 9', $text_domain),
__('Street 10', $text_domain),
),
);
}
add_filter('woocommerce_checkout_fields', 'custom_checkout_fields');
function custom_checkout_fields($fields) {
$text_domain = 'woocommerce';
$option_cities = array();
$select_areas = array('' => __('Select your street', $text_domain)); //
$test_areas = array('' => __('Select your street', $text_domain)); //
$test1_areas = array('' => __('Select your street', $text_domain)); //
$test2_areas = array('' => __('Select your street', $text_domain));
foreach (cities_areas_settings() as $city => $zone) {
$option_cities[$city] = $city;
if ($city === '') {
foreach ($zone as $area) {
$select_areas[$area] = $area;
}
} elseif ($city === 'Test') {
foreach ($zone as $area) {
$test_areas[$area] = $area;
}
} elseif ($city === 'Test1') {
foreach ($zone as $area) {
$test1_areas[$area] = $area;
}
} elseif ($city === 'Test2') {
foreach ($zone as $area) {
$test2_areas[$area] = $area;
}
}
}
$fields['billing']['billing_city']['type'] = 'select';
$fields['billing']['billing_city']['class'] = array('form-row-first');
$fields['billing']['billing_city']['input_class'] = array('state_select');
$fields['billing']['billing_city']['options'] = $option_cities;
$fields['shipping']['shipping_city']['type'] = 'select';
$fields['shipping']['shipping_city']['class'] = array('form-row-first');
$fields['shipping']['shipping_city']['input_class'] = array('state_select');
$fields['shipping']['shipping_city']['options'] = $option_cities;
$fields['billing']['billing_address_1'] = array(
'type' => 'select',
'label' => __('Street', $text_domain),
'class' => array('form-row-last'),
'input_class' => array('state_select'),
'options' => $select_areas,
'required' => true,
'default' => '',
'priority' => 50,
);
$fields['shipping']['shipping_address_1'] = array(
'type' => 'select',
'label' => __('Street', $text_domain),
'class' => array('form-row-last'),
'input_class' => array('state_select'),
'options' => $select_areas,
'required' => true,
'default' => '',
'priority' => 50,
);
return $fields;
}
add_action('wp_footer', 'custom_checkout_js_script');
function custom_checkout_js_script() {
if (is_checkout() && !is_wc_endpoint_url()) :
$text_domain = 'woocommerce';
$test_areas = array('' => __('Select your street', $text_domain));
$test1_areas = array('' => __('Select your street', $text_domain));
$test2_areas = array('' => __('Select your street', $text_domain));
$test3_areas = array('' => __('Select your street', $text_domain));
$settings = cities_areas_settings();
foreach ($settings['Test'] as $area) {
$test_areas[$area] = $area;
}
foreach ($settings['Test1'] as $area) {
$test1_areas[$area] = $area;
}
foreach ($settings['Test2'] as $area) {
$test2_areas[$area] = $area;
}
foreach ($settings['Test3'] as $area) {
$test3_areas[$area] = $area;
}
?>
<script language="javascript">
jQuery(function($) {
var a = 'select[name="billing_city"]',
b = 'select[name="billing_address_1"]',
l = <?php echo json_encode($test_areas); ?>,
o = <?php echo json_encode($test1_areas); ?>,
r = <?php echo json_encode($test2_areas); ?>,
i = <?php echo json_encode($test3_areas); ?>,
s = $(b).html();
function dynamicSelectOptions(opt) {
var options = '';
$.each(opt, function(key, value) {
options += '<option value="' + key + '">' + value + '</option>';
});
$(b).html(options);
}
if ($(a).val() === 'Test') {
dynamicSelectOptions(l);
} else if ($(a).val() === 'Test1') {
dynamicSelectOptions(o);
} else if ($(a).val() === 'Test2') {
dynamicSelectOptions(r);
} else if ($(a).val() === 'Test3') {
dynamicSelectOptions(i);
}
console.log($(a).val());
$('form.woocommerce-checkout').on('change', a, function() {
console.log($(this).val());
if ($(this).val() === 'Test') {
dynamicSelectOptions(l);
} else if ($(this).val() === 'Test1') {
dynamicSelectOptions(o);
} else if ($(this).val() === 'Test2') {
dynamicSelectOptions(r);
} else if ($(this).val() === 'Test3') {
dynamicSelectOptions(i);
} else {
$(b).html(s);
}
});
});
</script>
<script language="javascript">
jQuery(function($) {
var a = 'select[name="shipping_city"]',
b = 'select[name="shipping_address_1"]',
l = <?php echo json_encode($test_areas); ?>,
o = <?php echo json_encode($test1_areas); ?>,
r = <?php echo json_encode($test2_areas); ?>,
i = <?php echo json_encode($test3_areas); ?>,
s = $(b).html();
function dynamicSelectOptions(opt) {
var options = '';
$.each(opt, function(key, value) {
options += '<option value="' + key + '">' + value + '</option>';
});
$(b).html(options);
}
if ($(a).val() === 'Test') {
dynamicSelectOptions(l);
} else if ($(a).val() === 'Test1') {
dynamicSelectOptions(o);
} else if ($(a).val() === 'Test2') {
dynamicSelectOptions(r);
} else if ($(a).val() === 'Test3') {
dynamicSelectOptions(i);
}
console.log($(a).val());
$('form.woocommerce-checkout').on('change', a, function() {
console.log($(this).val());
if ($(this).val() === 'Test') {
dynamicSelectOptions(l);
} else if ($(this).val() === 'Test1') {
dynamicSelectOptions(o);
} else if ($(this).val() === 'Test2') {
dynamicSelectOptions(r);
} else if ($(this).val() === 'Test3') {
dynamicSelectOptions(i);
} else {
$(b).html(s);
}
});
});
</script>
<?php
endif;
}
The code works correctly on the checkout page.
Now I would display those selectable fields in the customer My Account Edit Address section and wake them work just like in the checkout page.
I looked at the answer here Change WooCommerce city fields to a dropdown in frontend and admin, but I can't figure out how to do it.
I tried to add the following hooks in the code:
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_city_fields' );
add_filter('woocommerce_admin_billing_fields', 'admin_order_pages_city_fields');
add_filter('woocommerce_admin_shipping_fields', 'admin_order_pages_city_fields');
add_filter( 'woocommerce_customer_meta_fields', 'custom_override_user_city_fields' );
But it is not working. How could I make those fields be displayed in the customer's profile?