Although UX and accessibility experts won’t like this customization, it’s still important to know “what’s possible” with WooCommerce.
In regard to the checkout form (billing + shipping + notes), there is a useful “woocommerce_checkout_fields” hook (filter) that is widely used by developers like me to alter the behavior of input fields.
In today’s episode we will take a look, indeed, at how to remove the checkout field labels from their default position (above fields), and use them as placeholders instead, so that we save up some vertical space.
Enjoy!
PHP Snippet: Display Field Labels Inside Input Boxes @ Checkout Page
/**
* @snippet Move Labels Inside Inputs - WooCommerce Checkout
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @compatible WooCommerce 5
* @community https://businessbloomer.com/club/
*/
add_filter( 'woocommerce_checkout_fields', 'bbloomer_labels_inside_checkout_fields', 9999 );
function bbloomer_labels_inside_checkout_fields( $fields ) {
foreach ( $fields as $section => $section_fields ) {
foreach ( $section_fields as $section_field => $section_field_settings ) {
$fields[$section][$section_field]['placeholder'] = $fields[$section][$section_field]['label'];
$fields[$section][$section_field]['label'] = '';
}
}
return $fields;
}
Hello,
Could you show us how to edit the code so that we can assign this to specific fields, rather than all fields?
Hi Gregory, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Awesome stuff! Now, if we can only get the asterisk to remain for required fields. ; )
🙂
Hello,
Possible to notify user if a field is required of not? As it looks like now it not very good UX. User do not know what fields are required and what are optional
WooCommerce should already change the border to red for required fields, can you see that? Let me know
I actually had the same question coming here. The missing fields are marked red if the user tries to checkout but he has no way of knowing in advance, which fields are considered required.
I am guessing Ryan Logan had the same question/issue above.
It seems to be a question of either having everything inline but no required field indicator or keeping everything as is to keep he asterisks. Is there any way out of this dilemma?
Hey Michael, I take the placeholder could take an “*” if the field is required, or you could style field borders via CSS. I’m afraid this is custom work – if you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hello all,
For me, the following solution worked well. It adds an asterix for required fields and (optional) for not-required fields. You can change it to whatever you need in the code. Hope it works for you too.
Thanks!