3

In Woocommerce i am trying to clear the checkout fields. so when a user that has ordered something before, and is now ordering something again, he/she will have to write in all his/her information again.

i am using this code

function clear_checkout_fields($input){
return '';
}

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

Now this code is clearing all the fields, but it also changes my VAT to show as 0.

does anyone know a solution to this?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
jamiemsm
  • 55
  • 4

1 Answers1

4

There is some arguments errors in your woocommerce_checkout_get_value hooked function.
There is in fact 2 arguments:

  • the $value argument that is returned as it is a filter hook,
  • the $imput argument that you can use to target any checkout field.

So in your case you will use the $imput argument, to avoid your custom VAT checkout field to be emptied. In the code below, you will need to replace vat_number by the correct field name attribute that is set in your custom VAT checkout field:

add_filter( 'woocommerce_checkout_get_value' , 'clear_checkout_fields' , 10, 2 );
function clear_checkout_fields( $value, $input ){
    if( $input != 'vat_number' )
        $value = '';
    
    return $value;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks! This worked! One follow up question. How would you do this for multiple fields? – Stanley Tan Jul 22 '20 at 00:55
  • 1
    @StanleyTan You can do ist with in_array() function like : `if ( ! in_array( $input, array( 'vat_number', 'something' ) ) ) {` or `if ( in_array( $input, array( 'vat_number', 'something' ) ) ) {` depending on what you want… – LoicTheAztec Jul 22 '20 at 01:14