5

I have renamed the "shipping_country" label of my woocommerce checkout page successfully using this code:

add_filter( 'woocommerce_checkout_fields', 'rename_woo_checkout_fields' );
function rename_woo_checkout_fields( $fields ) {
    $fields['shipping']['shipping_country']['label'] = 'Country'; 
    return $fields;
}

But when I try to add additional labels and placeholders which I want to change, it doesn't work. Well, something strange happens actually. When I refresh the page to apply the changes, it seems to work, but the page is still loading and after a second, reverts back to what it was originally. (the shipping_country field DOES still work, but all of the other fields I add the above happens.

I tried changing the sequence but it does not matter.

The fields I am trying to change which do not work are:

    $fields['billing']['billing_address_1']['label'] = 'Address';
    $fields['billing']['billing_address_1']['placeholder'] = 'Street and house number';
    $fields['billing']['billing_city']['label'] = 'City';
    $fields['billing']['billing_postcode']['label'] = 'Postcode';
    $fields['shipping']['shipping_postcode']['label'] = 'Postcode';
    $fields['shipping']['shipping_city']['label'] = 'City';
    $fields['shipping']['shipping_city']['placeholder'] = 'City';
    $fields['shipping']['shipping_address_1']['label'] = 'Address';
    $fields['shipping']['shipping_address_1']['placeholder'] = 'Street and house number';
    $fields['order']['order_comments']['placeholder'] = 'Special notes';

What could it be that is making the page revert the changes before it completes loading the page?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
J.K.
  • 167
  • 1
  • 10

1 Answers1

5

Try the following instead:

// For billing and shipping fields
add_filter( 'woocommerce_default_address_fields', 'custom_default_address_fields' );
function custom_default_address_fields( $address_fields ) {
    if ( is_checkout() ) {
        $address_fields['address_1']['label'] = __('Address', 'woocommerce');
        $address_fields['address_1']['placeholder'] = __('Street and house number', 'woocommerce');
        $address_fields['country']['label'] = __('Country', 'woocommerce');
        $address_fields['postcode']['label'] = __('Postcode', 'woocommerce');
        $address_fields['city']['label'] = __('City', 'woocommerce');
        $address_fields['city']['placeholder'] = __('City', 'woocommerce');
    }
    return $address_fields;
}

add_filter( 'woocommerce_checkout_fields', 'change_checkout_fields' );
function change_checkout_fields( $fields ) {
    $fields['order']['order_comments']['placeholder'] = __('Special notes');
    
    return $fields;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works on last WooCommerce version (5.0.0).

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks so much! It works perfect for most fields. Only the "order_comments" field is not changing. Is there a default hook for the order comments too? – J.K. Feb 14 '21 at 13:49
  • I found an old plugin file (woocommerce checkout field editor) which I deleted and now it works. Thanks a lot for your help @LoicTheAztec! – J.K. Feb 16 '21 at 13:48
  • 1
    It would be great if it was explained as to why it is happening which was part of the original question. I am having the same issue and the above code is not working. Some insight into what is causing it may hold the key as to how to solve it. – Murray Chapman Jun 14 '21 at 09:57