简体   繁体   中英

Woocommerce - Turn Off Autocomplete in Checkout Fields via "autocomplete=new-password"

I've found a couple of coding examples of this but nothing has worked. It seems such a simple thing to fix. We have an issue with our checkout page when certain users we're guessing have autocomplete on in their browser when checking out, we get duplicate or different Address 1 and 2 values. Even enabling the google api to help with autocomplete, didn't solve our issue. It corrected it some but, not enough.

This is one version of the solution I found in the interwebs melded with my own but, does not do anything....

/* Disable autofill address */

add_filter( 'woocommerce_form_field', 'change_autofill', 1, 1 );

function change_autofill( $field) {

$field = str_replace('autocomplete="billing_address_1"', 'autocomplete="new-password"', $field);
return $field; 

}

I would like the ability to pick and choose which input fields to set the autocomplete attribute to "new-password" as I've read this forces all modern browsers to think it's a password field and does not try to fill them. When using inspector, each input field in the checkout has "autocomplete=off" already. That's why we are after this solution, in this manner...

Was able to get this working fully with this code (I was searching the wrong thing):

/* Disable browser autofill on all Woocommerce form fields */

add_filter( 'woocommerce_form_field', 'change_autofill', 1, 1 );

function change_autofill( $field) {

$field = str_replace('autocomplete="off"', 'autocomplete="new-password"', $field);
return $field; 

}

This works because all fields are set with the attribute autocomplete=off it may not work for everyone. Use your inspector tool for each field to see. Whatever autocomplete is set to, is what you want your first start of str_replace searching for.

Cheers!

Just a comment regarding the approved answer, it should be:

$field = str_replace('autocomplete="new-password"', 'autocomplete="off"', $field);

instead of:

$field = str_replace('autocomplete="off"', 'autocomplete="new-password"', $field);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM