在结帐表单中将城镇/城市文本字段更改为“选择选项"字段 [英] Change town/city text field to Select Option field in checkout form
问题描述
在Woocommerce中,我想在选择字段选项字段中更改城镇/城市"文本字段.
In Woocommerce, I would like to change Town/City text field in a select field option field.
我该怎么办?
以下是屏幕截图:
谢谢
推荐答案
您需要先从'text'更改字段 type
使用专用挂钩 woocommerce_default_address_fields
来选择" .然后,您还必须将 标签
和更改为和要在其中设置城镇的 选项
自变量 键/值
的数组中.
You need first to change the field type
from 'text' to 'select' using the dedicated hook woocommerce_default_address_fields
. Then you have also to change the label
and to and an options
argument where you are going to set your towns in an array of key/values
.
在此数组中,您将按城镇划分一行,并用逗号分隔.
In this array, you will have a line by town separated by a coma.
这是代码:
add_filter( 'woocommerce_default_address_fields' , 'customize_checkout_city_field' );
function customize_checkout_city_field( $address_fields ) {
// Set HERE the cities (one line by city)
$towns_cities_arr = array(
'0' => __('Select your city', 'my_theme_slug'),
'paris' => 'Paris',
'versailles' => 'Versailles',
'cannes' => 'Cannes',
);
// Customizing 'billing_city' field
$address_fields['city']['type'] = 'select';
$address_fields['city']['class'] = array('form-row-last', 'my-custom-class'); // your class here
$address_fields['city']['label'] = __('Town / city', 'my_theme_slug');
$address_fields['city']['options'] = $towns_cities_arr;
// Returning Checkout customized fields
return $address_fields;
}
此代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.
该代码已经过测试并且功能齐全.
The code is tested and fully functional.
更新:要添加自定义类,请替换为 $ address_fields ['city'] ['class']…
类<您的strong> 我的自定义类"
.
Update: To add your custom class, replace in $address_fields['city']['class']…
the class 'my-custom-class'
by yours.
参考文献:
这篇关于在结帐表单中将城镇/城市文本字段更改为“选择选项"字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!