Skip to content
Advertisement

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement