首页 > 解决方案 > 即使 Woocommerce 电话号码和邮政编码字段的输入类型设置为“数字”,某些字母仍然会出现

问题描述

我设法使用此代码调整了我网站的结算结帐字段的一些内容

add_filter( 'woocommerce_default_address_fields', 'custom_woocommerce_default_address_fields' );
function custom_woocommerce_default_address_fields( $address_fields ) {
    $address_fields['first_name']['custom_attributes'] = array('maxlength' => 35);
    $address_fields['last_name']['custom_attributes'] = array('maxlength' => 35);
    $address_fields['address_1']['custom_attributes'] = array('maxlength' => 50);
    $address_fields['address_2']['custom_attributes'] = array('maxlength' => 50);
    $address_fields['city']['custom_attributes'] = array('maxlength' => 35);


    $address_fields['postcode']['custom_attributes'] = array('maxlength' => 4, 'type' => 'number' );
    $address_fields['phone']['custom_attributes'] = array('maxlength' => 11, 'type' => 'number' );

    unset($address_fields['company']);
    unset($address_fields['address_2']);

    return $address_fields;
}

请注意我在邮政编码和电话号码字段中所做的两件事。我都为这两个字段设置了特定的最大长度。我还将他们的输入类型设置为“数字”,因为我注意到在这些应该只是数字的字段中也可以输入字母。但是当我运行这段代码时没有任何反应。

我还尝试分离我为这些字段所做的自定义,如下所示:

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields )
{        
     $fields['billing']['billing_phone']['type'] = 'number';   
     $fields['billing']['billing_postcode']['type'] = 'number';
     $fields['billing']['billing_phone']['maxlength'] = 11;   
     $fields['billing']['billing_postcode']['maxlength'] = 4; 

     return $fields;    
}

但问题只是['type'] = 'number'是这两个结帐字段中的可见变化。我认为这没问题,但是当我测试输入所有字母时,字母er仍然出现在邮政编码和电话字段中。这是一个错误还是有可能克服这个问题?我只想在这两个计费字段中同时实现 maxlength 和 input type = number。

标签: validationwoocommercehook-woocommerce

解决方案


推荐阅读