skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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'); 
        }
    });
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search