简体   繁体   English

在 Woocommerce 结帐时禁用自动完成字段(自动填充),某些字段除外

[英]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:我使用以下代码禁用 woocommerce 结帐页面中的自动完成字段:

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?我想为账单国家和运输国家等特定字段启用自动完成功能如何?

You found the correct hook woocommerce_checkout_get_value .您找到了正确的钩子woocommerce_checkout_get_value You just had to add a callback function to it and write logic to return the value of your desire.您只需为其添加一个回调 function 并编写逻辑以返回您想要的值。

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.根据需要从$item_to_set_null数组中添加/删除项目。

Code is tested and WORKS.代码经过测试并且可以工作。

I recently found out that this can be done via javascript as well:我最近发现这也可以通过 javascript 完成:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM