Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 709
Alexey Svidentsov Avatar asked Aug 03 '16 08:08

Alexey Svidentsov


People also ask

How do I change my billing address in WooCommerce?

Changing your billing and shipping addresses in WooCommerce is easy – just log in to your WordPress site, go to the WooCommerce Settings page, and click on the Accounts tab. From there, you can choose whether you want to change your billing or shipping address, and then simply enter your new address and save changes.

How do I change the label in WooCommerce checkout?

Change the Checkout Label in the SettingsGo to WooCommerce > Settings > Checkout. From here, you can change the labels for the fields on the checkout page. Simply click on the field that you want to change, and then enter the new text in the “Label” field.


1 Answers

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
like image 194
LoicTheAztec Avatar answered Oct 07 '22 20:10

LoicTheAztec