[英]How to change Billing address field label in WooCommerce
In my design i have non standard billing fields label and markup. 在我的设计中,我有非标准的结算字段标签和标记。 For example "Town / City *" should be "Province *". 例如,“城镇/城市*”应为“省*”。
I have used WOO documentation, and filter woocommerce_billing_fields
. 我使用过WOO文档,并过滤woocommerce_billing_fields
。 And it works with class name, placeholder, create new fields. 它适用于类名,占位符,创建新字段。 But I cant reach label changed. 但我不能改变标签。
$out_arr['billing_city']['class'][0] = 'form-row-first';
$out_arr['billing_city']['label'] = __('Province', 'woocommerce');
$out_arr['billing_postcode']['label'] = __('Zipcode', 'woocommerce');
and using var_dump
of new $out_arr
array it shows correct labels 并使用新的$out_arr
数组的var_dump
它显示正确的标签
["billing_city"]=>
array(4) {
["label"]=>
string(8) "Province"
["required"]=>
bool(true)
["class"]=>
array(2) {
[0]=>
string(14) "form-row-first"
[1]=>
string(13) "address-field"
}
["autocomplete"]=>
string(14) "address-level2"
}
But i still have old labels in front-end. 但我仍然在前端有旧标签。 Any suggestions please? 有什么建议吗?
In specific cases you need to use the woocommerce_default_address_fields
filter. 在特定情况下,您需要使用woocommerce_default_address_fields
过滤器。 This filter is applied to all billing and shipping default fields: 此过滤器适用于所有结算和发货默认字段:
'country'
, 'first_name'
, 'last_name'
, 'company'
, 'address_1'
, 'address_2'
, 'city'
, 'state'
or 'postcode'
. 'country'
, 'first_name'
, 'last_name'
, 'company'
, 'address_1'
, 'address_2'
, 'city'
, 'state'
或'postcode'
。
Here we only use 'city'
and 'postcode'
as in your code: 在这里,我们只在代码中使用'city'
和'postcode'
:
add_filter( 'woocommerce_default_address_fields' , 'override_default_address_fields' );
function override_default_address_fields( $address_fields ) {
// @ for city
$address_fields['city']['class'] = array('form-row-first');
$address_fields['city']['label'] = __('Province', 'woocommerce');
// @ for postcode
$address_fields['postcode']['label'] = __('Zipcode', 'woocommerce');
return $address_fields;
}
This is tested and working. 这是经过测试和运作的。
This code snippet goes on function.php file of your active child theme or theme 此代码段位于您的活动子主题或主题的function.php文件中
References: 参考文献:
It's been a while since the OP question, but I faced a similar situation (we use "Department" instead of "Province") and found a solution that doesn't involve coding, which in my case makes things easier. OP问题已经有一段时间了,但我遇到了类似的情况(我们使用“部门”而不是“省”)并找到了一个不涉及编码的解决方案,在我看来这使得事情更容易。 It's the plugin Booster for WooCommerce . 这是WooCommerce的插件Booster 。
It ads several other customization possibilities I have yet to try, but tweaking labels works just fine. 它还宣传了我尚未尝试的其他几种定制可能性,但调整标签的工作正常。
BTW, depending on your server settings it may cause an out-of-memory error upon activation, with WordPress returning a blank page. 顺便说一句,根据您的服务器设置,它可能会导致激活时出现内存不足错误,WordPress会返回一个空白页面。 Temporarily increasing memory allocated to PHP allows it to complete activation. 临时增加分配给PHP的内存允许它完成激活。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.