Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom placeholder for all WooCommerce checkout fields

I am trying to add a placeholder to my WooCommerce checkout fields, and it's working perfectly for every field except for the phone and the email fields.

This is the code I am using:

add_filter('woocommerce_default_address_fields', 'override_address_fields');
function override_address_fields( $address_fields ) {
    $address_fields['first_name']['placeholder'] = 'Fornavn';
    $address_fields['last_name']['placeholder'] = 'Efternavn';
    $address_fields['address_1']['placeholder'] = 'Adresse';
    $address_fields['state']['placeholder'] = 'Stat';
    $address_fields['postcode']['placeholder'] = 'Postnummer';
    $address_fields['city']['placeholder'] = 'By';
    $address_fields['phone']['placeholder'] = 'Telefon';
    $address_fields['email']['placeholder'] = 'Email';
    return $address_fields;
}

Where am I going wrong? Why isn't the phone and email putting out any results?

I have taken the IDs from that two fields using my browser developer tool inspector.


Edit:

I have also tried this suggested code:

add_filter('woocommerce_checkout_fields', 'override_checkout_fields');
function override_checkout_fields( $checkout_fields ) {
    $checkout_fields['first_name']['placeholder'] = 'Fornavn';
    $checkout_fields['last_name']['placeholder'] = 'Efternavn';
    $checkout_fields['address_1']['placeholder'] = 'Adresse';
    $checkout_fields['state']['placeholder'] = 'Stat';
    $checkout_fields['postcode']['placeholder'] = 'Postnummer';
    $checkout_fields['city']['placeholder'] = 'By';
    $checkout_fields['phone']['placeholder'] = 'Telefon';
    $checkout_fields['email']['placeholder'] = 'Email';
    return $checkout_fields;
}

And this one too:

add_filter('woocommerce_checkout_fields', 'override_checkout_fields');
function override_checkout_fields( $checkout_fields ) {
    $checkout_fields['billing_first_name']['placeholder'] = 'Fornavn';
    $checkout_fields['billing_last_name']['placeholder'] = 'Efternavn';
    $checkout_fields['billing_address_1']['placeholder'] = 'Adresse';
    $checkout_fields['billing_state']['placeholder'] = 'Stat';
    $checkout_fields['billing_postcode']['placeholder'] = 'Postnummer';
    $checkout_fields['billing_city']['placeholder'] = 'By';
    $checkout_fields['billing_phone']['placeholder'] = 'Telefon';
    $checkout_fields['billing_email']['placeholder'] = 'Email';
    return $checkout_fields;
}

But it doesn't work.

like image 802
eMikkelsen Avatar asked Sep 20 '17 15:09

eMikkelsen


2 Answers

Looking at the official docs, you will see that there is no 'phone' and 'email' key fields for the default addresses when using woocommerce_default_address_fields filter hook.
Accepted fields keys are:
country, first_name, last_name, company, address_1, address_2, city, state, postcode.

This is why you can get changes using woocommerce_default_address_fields

Email and phone are billing fields and they are available trough woocommerce_checkout_fields filter hook. They are named (see in the documentation) 'billing_phone' and 'billing_phone'

The correct way to override them is:

add_filter( 'woocommerce_checkout_fields' , 'override_billing_checkout_fields', 20, 1 );
function override_billing_checkout_fields( $fields ) {
    $fields['billing']['billing_phone']['placeholder'] = 'Telefon';
    $fields['billing']['billing_email']['placeholder'] = 'Email';
    return $fields;
}

And for the others fields (billing and shipping):

add_filter('woocommerce_default_address_fields', 'override_default_address_checkout_fields', 20, 1);
function override_default_address_checkout_fields( $address_fields ) {
    $address_fields['first_name']['placeholder'] = 'Fornavn';
    $address_fields['last_name']['placeholder'] = 'Efternavn';
    $address_fields['address_1']['placeholder'] = 'Adresse';
    $address_fields['state']['placeholder'] = 'Stat';
    $address_fields['postcode']['placeholder'] = 'Postnummer';
    $address_fields['city']['placeholder'] = 'By';
    return $address_fields;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

All code is tested on Woocommerce 3+ and works.


Reference: Customizing checkout fields using actions and filters


For the login form fields: Customize WooCommerce login form user fields

like image 159
LoicTheAztec Avatar answered Oct 27 '22 00:10

LoicTheAztec


per the official documentation https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/ Here you have the updated code:

 add_filter( 'woocommerce_checkout_fields' , 'override_billing_checkout_fields', 20, 1 );
 function override_billing_checkout_fields( $fields ) {

     $fields['billing']['billing_first_name']['placeholder'] = 'Prénom';
     $fields['billing']['billing_last_name']['placeholder'] = 'Nom';
     $fields['billing']['billing_company']['placeholder'] = 'Nom de la société (optionnel)';
     $fields['billing']['billing_postcode']['placeholder'] = 'Code postal';
     $fields['billing']['billing_phone']['placeholder'] = 'Téléphone';
     $fields['billing']['billing_city']['placeholder'] = 'Ville';


     $fields['shipping']['shipping_first_name']['placeholder'] = 'Prénom';
     $fields['shipping']['shipping_last_name']['placeholder'] = 'Nom';
     $fields['shipping']['shipping_company']['placeholder'] = 'Nom de la société (optionnel)';
     $fields['shipping']['shipping_postcode']['placeholder'] = 'Code postal';
     $fields['shipping']['shipping_phone']['placeholder'] = 'Téléphone';
     $fields['shipping']['shipping_city']['placeholder'] = 'Ville';

     return $fields;
 }

Result: enter image description here

like image 21
Alan Avatar answered Oct 26 '22 22:10

Alan