You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WooCommerce中将自定义结账字段从文本框改为文本域

把WooCommerce结账页的billing_options文本框改为文本域的方法

情况1:字段是你自己通过代码添加的

直接修改字段类型,通过woocommerce_checkout_fields钩子实现即可:

add_filter( 'woocommerce_checkout_fields', 'change_billing_options_to_textarea' );
function change_billing_options_to_textarea( $fields ) {
    // 确认目标字段存在
    if ( isset( $fields['billing']['billing_options'] ) ) {
        // 将字段类型从text改为textarea
        $fields['billing']['billing_options']['type'] = 'textarea';
        // 可选:设置文本域行数、保留原占位符
        $fields['billing']['billing_options']['custom_attributes'] = array(
            'rows' => '4',
            'placeholder' => 'توضیحات (اختیاری)'
        );
    }
    return $fields;
}

情况2:字段由第三方插件生成

如果字段是插件自动添加的,用woocommerce_form_field_{field_name}钩子覆盖原有输出:

add_action( 'woocommerce_form_field_billing_options', 'override_billing_options_to_textarea', 10, 4 );
function override_billing_options_to_textarea( $field, $key, $args, $value ) {
    // 仅在结账页面生效
    if ( is_checkout() && ! is_wc_endpoint_url() ) {
        ob_start();
        ?>
        <p class="form-row <?php echo esc_attr( implode( ' ', $args['class'] ) ); ?>" id="<?php echo esc_attr( $key ); ?>_field">
            <?php if ( ! empty( $args['label'] ) ) : ?>
                <label for="<?php echo esc_attr( $key ); ?>" class="<?php echo esc_attr( implode( ' ', $args['label_class'] ) ); ?>"><?php echo esc_html( $args['label'] ); ?> <?php echo $args['required'] ? '&nbsp;<abbr class="required" title="' . esc_attr__( 'required', 'woocommerce' ) . '">*</abbr>' : ''; ?></label>
            <?php endif; ?>
            <textarea name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" class="input-text <?php echo esc_attr( implode( ' ', $args['input_class'] ) ); ?>" placeholder="<?php echo esc_attr( $args['placeholder'] ); ?>" rows="4"><?php echo esc_textarea( $value ); ?></textarea>
            <?php if ( ! empty( $args['description'] ) ) : ?>
                <span class="description"><?php echo wp_kses_post( $args['description'] ); ?></span>
            <?php endif; ?>
        </p>
        <?php
        $field = ob_get_clean();
    }
    return $field;
}

额外步骤:确保字段数据正常保存与显示

如果之前没处理过字段数据的存储,添加以下代码让内容能存入订单:

add_action( 'woocommerce_checkout_update_order_meta', 'save_billing_options_value' );
function save_billing_options_value( $order_id ) {
    if ( ! empty( $_POST['billing_options'] ) ) {
        update_post_meta( $order_id, '_billing_options', sanitize_textarea_field( $_POST['billing_options'] ) );
    }
}

要在后台订单详情页显示该字段内容,添加:

add_action( 'woocommerce_admin_order_data_after_billing_address', 'show_billing_options_in_admin' );
function show_billing_options_in_admin( $order ) {
    $options = get_post_meta( $order->get_id(), '_billing_options', true );
    if ( $options ) {
        echo '<p><strong>توضیحات:</strong> ' . wp_kses_post( $options ) . '</p>';
    }
}

使用方法

  • 将上述代码添加到当前主题的functions.php文件末尾,或通过「Code Snippets」这类自定义代码插件添加(推荐后者,避免主题更新丢失代码)。
  • 保存后刷新结账页面,即可看到billing_options字段已变为文本域。

内容的提问来源于stack exchange,提问作者user15015903

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 06:43:28