将 Woocommerce 结帐 "Order Comments" 更改为具有已定义最大长度的文本输入字段

Change Woocommerce checkout "Order Comments" to a text input field with a defined maxlength

我想将 Woocommerce 中的 "Order Comments" 字段从文本框更改为最大字符数为 18 的输入框。并将名称更改为 "Order Instructions"

我找到了一种使用以下代码编辑 functions.php 字段的方法:

// remove Order Notes from checkout field in Woocommerce
add_filter( 'woocommerce_checkout_fields' , 'alter_woocommerce_checkout_fields' );
function alter_woocommerce_checkout_fields( $fields ) {
    unset($fields['order']['order_comments']);
    return $fields;
}

这可以用 functions.php 中的过滤器来完成吗?

更新:您可以将以下内容用于 "Order Comments" 结帐字段,以:

  • 将字段类型更改为输入文本
  • 设置最大长度
  • 将标签更改为 "Order Instructions"

这是代码

// Change Order Notes type to 'text' on Woocommerce checkout
add_filter( 'woocommerce_checkout_fields' , 'alter_woocommerce_checkout_fields' );
function alter_woocommerce_checkout_fields( $fields ) {
    // Change field type
    $fields['order']['order_comments']['type'] = 'text';

    // Limit to a max length
    $fields['order']['order_comments']['custom_attributes'] = array('maxlength' => 18);

    // Change the label name
    $fields['order']['order_comments']['label'] = __('Order Instructions', "woocommerce");

    return $fields;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。