Woocommerce结帐字段最大长度,仅输入数字字段(邮政编码)


Woocommerce checkout field maxlength, make input number field only (postcode)

我已经为此苦苦挣扎了几天了。我想做的是,它将邮政编码/邮政编码字段限制为 4 个数字。因此,要向这些字段添加最大长度类型="数字"。

到目前为止我尝试过的事情:

  1. (功能.php)

    add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
    function custom_override_checkout_fields( $fields ) {    
      $fields['billing']['postcode']['maxlength'] = 4;    
      return $fields;
    }
    
  2. (functions.php) a jQuery way

(由于某种代码格式错误,无法发布?

编辑

:事实证明,我正在编辑服务器上的旧文件......编辑正确的代码后,最大长度更改与先前提供的代码一样工作正常。但是仍然存在更改输入类型的问题。

我尝试添加$fields['billing']['billing_postcode']['type'] = 'number';但出于某种原因,这似乎一起删除了输入字段。可能是由于我猜的一些错误。不确定。

您需要

在名为"custom_attributes"的进一步子数组中添加属性,例如:

$fields['billing']['postcode']['custom_attributes']['maxlength'] = 4;