Home » Php » php – Remove WooCommerce checkout fields values

php – Remove WooCommerce checkout fields values

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m trying to apply autocomplete="off" on my WooCommerce checkout fields but it just doesn’t work.

Is there a way to do that for the checkout form?

I checked the docs and there’s nothing available there. I also tried setting the default value to empty but it doesn’t work either. This is when the user is not logged in of course.

enter image description here

UPDATE:

I tried unset as suggested by @smvax but it did not work as well.

add_filter('woocommerce_checkout_fields', 'default_values_checkout_fields');
function default_values_checkout_fields($fields) {
  if (!is_user_logged_in()) {
      unset($fields['billing_city']);
      unset($fields['billing_first_name']);
      unset($fields['billing_last_name']);
      unset($fields['billing_company']);
      unset($fields['billing_address_1']);
      unset($fields['billing_address_2']);
      unset($fields['billing_city']);
      unset($fields['billing_postcode']);
      unset($fields['billing_country']);
      unset($fields['billing_state']);
      unset($fields['billing_email']);
      unset($fields['billing_phone']);
      unset($fields['shipping_city']);
      unset($fields['shipping_first_name']);
      unset($fields['shipping_last_name']);
      unset($fields['shipping_company']);
      unset($fields['shipping_address_1']);
      unset($fields['shipping_address_2']);
      unset($fields['shipping_postcode']);
      unset($fields['shipping_country']);
      unset($fields['shipping_state']);
      return $fields;
  }
}

I also tried the answer here but it’s not working as well.

Thanks

How to&Answers:

You can use WordPress '__return_empty_string' with woocommerce_checkout_get_value WooCommerce filter hook to get empty values simply this way:

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

This will empty all checkout values, when checkout page is loaded.

The Code goes in functions.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works.

You should reset all existing sessions in Woocommerce settings > System status > tools and also the cache if your e-commerce use any.
Also empty your browser cache and stored data.

Related: Clear only some checkout fields values in Woocommerce