【发布时间】:2021-08-20 10:05:19
【问题描述】:
我使用以下代码禁用 woocommerce 结帐页面中的自动完成字段:
add_filter('woocommerce_checkout_get_value','__return_empty_string',10);
以上代码禁用所有自动完成字段。我想为账单国家和送货国家等特定字段启用自动完成功能如何?
【问题讨论】:
标签: php wordpress woocommerce
我使用以下代码禁用 woocommerce 结帐页面中的自动完成字段:
add_filter('woocommerce_checkout_get_value','__return_empty_string',10);
以上代码禁用所有自动完成字段。我想为账单国家和送货国家等特定字段启用自动完成功能如何?
【问题讨论】:
标签: php wordpress woocommerce
您找到了正确的钩子woocommerce_checkout_get_value。您只需为其添加一个回调函数并编写逻辑以返回您想要的值。
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;
}
根据需要从$item_to_set_null 数组中添加/删除项目。
代码已经过测试并且可以工作。
【讨论】:
我最近发现这也可以通过 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');
}
});
【讨论】: