答案 0 :(得分:6)
可以使用隐藏在 woocommerce_checkout_fields
操作挂钩中的自定义函数来完成,您可以在数组中定义所需的城市:
// Change "city" checkout billing and shipping fields to a dropdown
add_filter( 'woocommerce_checkout_fields' , 'override_checkout_city_fields' );
function override_checkout_city_fields( $fields ) {
// Define here in the array your desired cities (Here an example of cities)
$option_cities = array(
'' => __( 'Select your city' ),
'Karachi' => 'Karachi',
'Lahore' => 'Lahore',
'Faisalabad' => 'Faisalabad',
'Rawalpindi' => 'Rawalpindi',
'Gujranwala' => 'Gujranwala',
'Peshawar' => 'Peshawar',
'Multan' => 'Multan',
'Hyderabad' => 'Hyderabad',
'Islamabad' => 'Islamabad'
);
$fields['billing']['billing_city']['type'] = 'select';
$fields['billing']['billing_city']['options'] = $option_cities;
$fields['shipping']['shipping_city']['type'] = 'select';
$fields['shipping']['shipping_city']['options'] = $option_cities;
return $fields;
}
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
经过测试和工作。