
Today I was dealing with a “little but annoying” thing. I found that there is an autofocus function on the First Name field under WooCommerce billing and shipping address. In a normal condition, maybe it’s not a problem. But when you did a customisation, for example : Re-order the fields, add some custom fields on the top and put the First Name to the middle or to the bottom, it doesn’t make sense to have the autofocus active.
As usual, I went to Uncle Google and found some threads. I found this and this. But unfortunately, all of those solutions are didn’t work for me. I guess because both of them are using woocommerce_checkout_fields
hook to call their function.
Finally, I wrote this code and it works:
<?php | |
/* Disable autofocus on the billing First Name field */ | |
add_filter('woocommerce_billing_fields','disable_autofocus_billing_firstname'); | |
function disable_autofocus_billing_firstname($fields) { | |
$fields['billing_first_name']['autofocus'] = false; | |
return $fields; | |
} | |
/* Disable autofocus on the shipping First Name field */ | |
add_filter('woocommerce_shipping_fields','disable_autofocus_shipping_firstname'); | |
function disable_autofocus_shipping_firstname($fields) { | |
$fields['shipping_first_name']['autofocus'] = false; | |
return $fields; | |
} |
Basically, by default WooCommerce will set an autofocus on the billing first name and shipping first name field. If you want to disable this, you need to set the autofocus into false and put it under woocommerce_billing_fields
and woocommerce_shipping_fields
hook.
If you have any question or feedback, do not hesitate to leave your comment on the form below. 🙂
0 Comments