Custom placeholder for all WooCommerce checkout fields

▼魔方 西西 提交于 2020-06-25 03:29:10

问题


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.


回答1:


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




回答2:


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:




回答3:


Looking through the WooCommerce documentation you may want to try using the woocommerce_checkout_fields filter instead of woocommerce_default_address_fields

Seems more logical as the fields above aren't all address related, and that hook derived from class-wc-countries.php.

Also, note the field names are different based on the context of the page, for example, the billing page fields are named as so:

  • billing_first_name
  • billing_last_name
  • billing_email
  • billing_phone

Hope that helps! Good luck.



来源:https://stackoverflow.com/questions/46326620/custom-placeholder-for-all-woocommerce-checkout-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!