【问题标题】:Disable autocomplete phone billing Woocommerce禁用自动完成电话计费 Woocommerce
【发布时间】:2021-05-09 22:12:56
【问题描述】:

首先,我自己不是编码员,因此我与您分享的代码是我自己创建的,通过在这里和那里复制来尝试使其工作......但事实并非如此。那你能帮帮我吗?

我正在尝试:将 billing_phone 设为 autocomplete="tel" 值,变为 automplete="nope",这样它将停止自动完成该文件,并且只自动完成该文件。

在过去的 10 个小时里我尝试了很多东西,但都没有运气,所以这是我现在在 functions.php 文件中得到的结果,虽然它可能完全错误,但我只是希望你可以帮助我,当然,欢迎您解释您的代码:

/* Disable autofill phone */
add_action('woocommerce_billing_fields', 'autocomplete_nope');
function autocomplete_nope( $content ) {
    $str_pos = strpos( $content, 'name="billing_phone"' );
    $content = substr_replace( $content, 'value autocomplete="nope"', $str_pos, 0 );
return $content;
}

更新

根据您下面的回答,我尝试了这 2 个选项,但仍然没有运气,他们仍然在 HTML 中发回这个:

<input type="tel" class="input-text wfacp-form-control" name="billing_phone" id="billing_phone" placeholder="999-999-9999" value="" autocomplete="tel">

第一次尝试(不工作):

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields )
{        
     $fields['billing']['billing_phone']['custom_attributes'] = array( "autocomplete" => "nope" );      
     return $fields;    
}

更新 - 第二次尝试(无效):

add_action('woocommerce_billing_fields', 'autocomplete_nope');
function autocomplete_nope( $fields ) {
    $fields['billing']['billing_phone']['autocomplete'] = false;
    return $fields;
}

非常感谢您的宝贵时间。

【问题讨论】:

  • 能否请您检查一下 - stackoverflow.com/questions/31653634/…。也许对你有帮助。
  • 在你的钩子函数中试试这个(在里面删除你的代码):$fields['billing']['billing_phone']['autocomplete'] = false; 其中变量$content应该被$fields替换。
  • 建议的链接似乎与我需要的非常相似,但也无法使其正常工作。 Loic,我已经完成了(或者至少认为这是您的建议)但仍然无法正常工作,请检查我的问题的编辑,看看这是否是您的意思。
  • 我已经更新了你最后的代码……现在就试试吧
  • 仍然没有运气,虽然我为我的商店使用了一个特定的插件,但它下面使用了 woocommerce,所以我认为这不会影响。但是你的代码的结果仍然是一样的。

标签: php wordpress woocommerce autofill


【解决方案1】:

终于让它工作了,不完全确定下面的代码做了什么,但我设法让它工作:

/* Disable autofill phone */
function change_autofill( $field, $key, $args, $value ) {
    //  Remove the .form-row class from the current field wrapper
    $field = str_replace('autocomplete="tel"', 'autocomplete="nope"', $field);
    //  Wrap the field (and its wrapper) in a new custom div, adding .form-row so the reshuffling works as expected, and adding the field priority
    $field = '<div autocomplete="none" data-priority="' . $args['priority'] . '">' . $field . '</div>';
    return $field;
}
add_filter( 'woocommerce_form_field', 'change_autofill', 10, 4 );

【讨论】:

    【解决方案2】:

    字段自动完成的问题远远超出了 WooCommerce 的范畴。有 an ancient (and large) thread discussing this topic 以及 Google 多年来如何使用 Chrome 处理它。试试这个代码:

    /* Disable autofill phone */
    
    add_filter( 'woocommerce_form_field', 'change_autofill', 1, 1 );
    
    function change_autofill( $field) {
        
        $agent = $_SERVER['HTTP_USER_AGENT'];
        
        if (strpos($agent, 'Firefox') !== false) {
            $field = str_replace('autocomplete="tel"', 'autocomplete="off"', $field);
            return $field;
        }   
        else {
            $field = str_replace('autocomplete="tel"', 'autocomplete="none"', $field);
            return $field;
        }      
        
    }
    

    截至 2021 年 6 月,这应该适用于大多数使用的网络浏览器。请注意,这可能会随着来自 Google、Mozilla、Apple 等的未来更新而改变(就像以前发生的那样)。

    【讨论】:

      猜你喜欢
      • 2021-12-27
      • 1970-01-01
      • 2018-03-09
      • 1970-01-01
      • 2021-03-02
      • 2021-10-02
      • 1970-01-01
      • 2018-08-28
      • 2010-11-27
      相关资源
      最近更新 更多