'Disable auto-complete fields (auto-fill) on Woocommerce checkout except for some fields

I use below code to disable Autocomplete Fields in the woocommerce checkout page:

add_filter('woocommerce_checkout_get_value','__return_empty_string',10);

Above code disables all autocomplete fields. How about I want to enable autocomplete for specific fields like Billing Country and Shipping Country?



Solution 1:[1]

You found the correct hook woocommerce_checkout_get_value. You just had to add a callback function to it and write logic to return the value of your desire.

add_filter( 'woocommerce_checkout_get_value', 'bks_remove_values', 10, 2 );

function bks_remove_values( $value, $input ) {
    $item_to_set_null = array(
            'billing_first_name',
            'billing_last_name',
            'billing_company',
            'billing_address_1',
            'billing_address_2',
            'billing_city',
            'billing_postcode',
            'billing_country',
            'billing_state',
            'billing_email',
            'billing_phone',
            'shipping_first_name',
            'shipping_last_name',
            'shipping_company',
            'shipping_address_1',
            'shipping_address_2',
            'shipping_city',
            'shipping_postcode',
            'shipping_country',
            'shipping_state',
        ); // All the fields in this array will be set as empty string, add or remove as required.

    if (in_array($input, $item_to_set_null)) {
        $value = '';
    }

    return $value;
}

Add/Remove items from $item_to_set_null array as you require.

Code is tested and WORKS.

Solution 2:[2]

I recently found out that this can be done via javascript as well:

$(document).ready(function(){
    if (window.location.href.indexOf("checkout") > -1) {
  $("#billing_first_name").removeAttr('placeholder value');
  $("#billing_last_name").removeAttr('placeholder value');
  $("#billing_email").removeAttr('placeholder value');
  $("#billing_phone").removeAttr('placeholder value');
  $("#billing_company").removeAttr('placeholder value');
  $("#billing_address_1").removeAttr('placeholder value');
  $("#billing_address_2").removeAttr('placeholder value');
  $("#billing_city").removeAttr('placeholder value');
  $("#billing_postcode").removeAttr('placeholder value'); 
  $("#shipping_first_name").removeAttr('placeholder value'); 
  $("#shipping_last_name").removeAttr('placeholder value'); 
  $("#shipping_company").removeAttr('placeholder value'); 
  $("#shipping_address_1").removeAttr('placeholder value'); 
  $("#shipping_address_2").removeAttr('placeholder value'); 
  $("#shipping_city").removeAttr('placeholder value'); 
  $("#shipping_postcode").removeAttr('placeholder value'); 
    }
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bhanu
Solution 2 Rosalito Udtohan