Clear only some checkout fields values in Woocommerce

不打扰是莪最后的温柔 提交于 2020-08-07 09:42:09

问题


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?


回答1:


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 bellow, 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.



来源:https://stackoverflow.com/questions/50795010/clear-only-some-checkout-fields-values-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!