Search code examples
phpwordpresswoocommercecheckoutwoocommerce-checkout-fields

Customized Woocommerce checkout fields reverting to default after page load


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?


Solution

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