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

Ros*_*han 3 php wordpress woocommerce

我使用下面的代码禁用 woocommerce 结帐页面中的自动完成字段:

add_filter('woocommerce_checkout_get_value','__return_empty_string',10);
Run Code Online (Sandbox Code Playgroud)

上面的代码禁用所有自动完成字段。我想为帐单国家/地区和送货国家/地区等特定字段启用自动完成功能怎么样?

bha*_*anu 6

您找到了正确的钩子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;
}
Run Code Online (Sandbox Code Playgroud)

$item_to_set_null根据需要从数组中添加/删除项目。

代码经过测试并且可以工作。