Can’t change a label in woocommerce with the normal filter

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

2 Answers
2

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.

Leave a Comment