1
  // Hook in
  add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
 $fields['order']['order_comments']['placeholder'] = 'Special delivery requirements';
 $fields['billing']['billing_company']['label'] = 'Company name if applicable';
 $fields['shipping']['shipping_company']['label'] = 'Company name if applicable';
 $fields['billing']['billing_address_2']['placeholder'] = 'House number / name';
 $fields['shipping']['shipping_address_2']['placeholder'] = 'House number / name';
 $fields['billing']['billing_state']['required'] = true;
 $fields['billing']['billing_state']['label'] = 'County <abbr class="required" title="required">*</abbr>';
 return $fields;
}

I've used thIs code to change some fields for the woocommerce checkout form.

For some inexplicable reason, the label for the billing state refuses to change. Even though the label for billing company had changed the successfully, the label for the billing state refuses to. It has successfully become required but I can't change the label.

Does anyone know what I am doing wrong or if this is a bug.

  • Can you check if <abbr> tag is the cause, as it is used for abbreviation, try using, for example <span> instead ... Just a guess – Maruti Mohanty Oct 31 '13 at 12:01
  • @MarutiMohanty no doesnt work at all. Can you reproduce the error? – Lucky Luke Oct 31 '13 at 13:38
9

You have to use the woocommerce_default_address_fields hook:

add_filter( 'woocommerce_default_address_fields' , 'wpse_120741_wc_def_state_label' );
function wpse_120741_wc_def_state_label( $address_fields ) {
     $address_fields['state']['label'] = 'County';
     return $address_fields;
}

This (and more) is described at the woocommerce docs: Customizing checkout fields using actions and filters, so read your stuff next time thoroughly... ;)


Edit:
Above code isn't working for all cases, because on country selection the state field of the checkout form gets updated via javascript. To make it work it's necessary to add the desired name for those cases as translation, using the woocommerce_get_country_locale hook like this:

add_filter('woocommerce_get_country_locale', 'wpse_120741_wc_change_state_label_locale');
function wpse_120741_wc_change_state_label_locale($locale){
    // uncomment /** like this /**/ to show the locales
    /**
    echo '<pre>';
    print_r($locale);
    echo '</pre>';
    /**/
    $locale['GB']['state']['label'] = __('test', 'woocommerce');
    $locale['US']['state']['label'] = __('anything', 'woocommerce');
    return $locale;
}

The hook is located at the class-wc-countries.php in the get_country_locale() function.

| improve this answer | |
  • 1
    It's probably useful to point out that the ajax call to reload section of the checkout form is what overwrites the originally correctly overwritten form field labels with values that are not overwritten. Why those wouldn't use the same hook in some way or shape beats me. – kontur Oct 5 '17 at 10:15
  • @NeophytePolyhistor My pleasure – Nicolai Oct 6 '17 at 11:18
  • @kontur Especially four years later – Nicolai Oct 6 '17 at 11:19
0

I know this was some time ago but I would like to add a little to this thread if only to save some others the same frustration I experienced.

There are some fields in the checkout page that just won't change regardless of the filter you use these fields are due to the checkout.js file. This file loads locale information from the current locale and resets the fields labels and placeholder those values. this was particularly frustrating when trying to change the placeholder "Street address" to PO Box/Street address

What needs to be done to change these fields is to set a locale in you wp_config.php file something like:

define('WPLANG', 'en_GB');

Then download the plugin "codestyling localisation"

go to Tools->Localization, scroll down to woocommerce and rescan your language file, in this case it would be the English/United Kingdom file.

After it has scanned click edit and search for your term, in this case "Street address" and add your translation in my case that was "Street address/PO Box".

click the "generate mo-file" button at the top and you are done, full instructions on how to do this at:

http://docs.woothemes.com/document/woocommerce-localization/

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.