Skip to content

Instantly share code, notes, and snippets.

@gerharddt
Created September 8, 2020 07:36
Show Gist options
  • Save gerharddt/bc733c31e5904805f9da081b3eb58b91 to your computer and use it in GitHub Desktop.
Save gerharddt/bc733c31e5904805f9da081b3eb58b91 to your computer and use it in GitHub Desktop.
<?php
// change the checkout city field to a dropdown field
function nfh_change_city_to_dropdown( $fields ) {
$cities = array(
'',
'Nairobi',
'Nakuru',
'Eldorate',
'Mombasa',
'Kisumu',
'Diani',
'Malindi',
'Nyanyuki',
'Nyeri'
// etc
);
$city_args = wp_parse_args( array(
'type' => 'select',
'options' => array_combine( $cities, $cities ),
), $fields['shipping']['shipping_city'] );
$fields['shipping']['shipping_city'] = $city_args;
$fields['shipping']['shipping_city']['class'][1] = 'select_city';
$fields['billing']['billing_city'] = $city_args;
$fields['billing']['billing_city']['class'][1] = 'select_city';
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'nfh_change_city_to_dropdown' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment