How to change Billing address field label in WooCommerce

此生再无相见时 提交于 2020-11-27 02:49:49

问题


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. 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

["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?


回答1:


In specific cases you need to use the woocommerce_default_address_fields filter. 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'.

Here we only use 'city' and 'postcode' as in your code:

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

References:

  • Official WooThemes - Customizing checkout fields using actions and filters
  • WooCommerce - Overriding billing state and post code on existing checkout fields



回答2:


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. It's the plugin Booster for WooCommerce.

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. Temporarily increasing memory allocated to PHP allows it to complete activation.



来源:https://stackoverflow.com/questions/38738394/how-to-change-billing-address-field-label-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!