我使用此代码来更改一些结账地址字段,以反映某些国家/地区更正确的地址格式。其他国家/地区的字段有效,但英国和新西兰国家标签更改部分不起作用。英国的县(可选)字段仍然具有相同的标签,新西兰的地区(可选)字段仍然是具有相同标签的下拉列表。我有一种感觉,WC 核心的 javascript 可能会覆盖这里的某些内容。有什么想法吗?
function modify_default_address_fields($address_fields) {
if ( function_exists('WC') && WC()->customer && ( is_checkout() || is_account_page() ) ) {
$country = WC()->customer->get_billing_country();
// Taiwan
if ($country === 'TW') {
$address_fields['city']['label'] = 'District';
$address_fields['state']['label'] = 'City';
}
// Hong Kong
if ($country === 'HK') {
$address_fields['address_1']['label'] = 'Flat/Unit, Floor, Building, Street';
$address_fields['city']['label'] = 'District';
unset($address_fields['postcode']);
}
// Germany
if ($country === 'DE') {
unset($address_fields['state']);
}
// UK
if ($country === 'GB') {
$address_fields['city']['label'] = 'Locality / Village name (optional)';
$address_fields['state']['required'] = true;
$address_fields['state']['label'] = 'Town / City';
}
// New Zealand
if ($country === 'NZ') {
$address_fields['city']['label'] = 'Suburb';
$address_fields['state']['required'] = true;
$address_fields['state']['label'] = 'Town / City';
$address_fields['state']['type'] = 'text';
}
}
return $address_fields;
}
// Make sure all systems are loaded before executing the function (prevent fatal error)
function initialize_address_field_modifications() {
add_filter('woocommerce_default_address_fields', 'modify_default_address_fields', 9999);
}
add_action('init', 'initialize_address_field_modifications');
为此,你没有使用右钩拳。请尝试以下方法:
add_filter('woocommerce_get_country_locale', 'alter_addresses_fields_based_on_country',);
function alter_addresses_fields_based_on_country($locale) {
$hide_field = array('required' => false, 'hidden' => true );
// Taiwan
$locale['TW']['city']['label'] = __('District', 'woocommerce');
$locale['TW']['state']['label'] = __('City', 'woocommerce');
// Hong Kong
$locale['HK']['address_1']['label'] = __('Flat/Unit, Floor, Building, Street', 'woocommerce');
$locale['HK']['city']['label'] = __('District', 'woocommerce');
$locale['HK']['postcode'] = $hide_field;
// Germany
$locale['DE']['state'] = $hide_field;
// United Kingdom
$locale['GB']['city'] = array(
'required' => false,
'label' => __('Locality / Village name', 'woocommerce')
);
$locale['GB']['state'] = array(
'required' => true,
'label' => __('Town / City', 'woocommerce')
);
// New Zealand
$locale['NZ']['city']['label'] = __('Suburb', 'woocommerce');
$locale['NZ']['state'] = array(
'required' => true,
'label' => __('Town / City', 'woocommerce'),
'type' => __('text', 'woocommerce'),
);
return $locale;
}
代码位于活动子主题的functions.php 文件中(或插件中)。已测试并工作。
相关:请参阅 WC_Countries
get_country_locale()
方法,使用一组按国家/地区设置规则的特定字段。