在 Woocommerce 注册表和管理员编辑用户中添加一个字段 [英] Add a field to Woocommerce registration form and in admin edit user

查看:19
本文介绍了在 Woocommerce 注册表和管理员编辑用户中添加一个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WooCommerce 中,我在帐户注册表中添加了额外的字段:

In WooCommerce, I have added extra field to account registration form:

// 1. ADD FIELDS
add_action( 'woocommerce_register_form_start', 'add_woo_account_registration_field' );
function add_woo_account_registration_field() {
    ?>

    <p class="form-row form-row-first">
    <label for="reg_billing_account_number"><?php _e( 'Ship to/ Account number', 'woocommerce' ); ?> <span class="required">*</span></label>
    <input type="text" class="input-text" name="billing_account_number" id="reg_billing_account_number" value="<?php if ( ! empty( $_POST['billing_account_number'] ) ) esc_attr_e( $_POST['billing_account_number'] ); ?>" />
    </p>

    <div class="clear"></div>

    <?php
}

// 2. VALIDATE FIELDS
add_filter( 'woocommerce_registration_errors', 'validate_woo_account_fields', 10 );
function validate_woo_account_fields( $errors, $username, $email ) {
    if ( isset( $_POST['billing_account_name'] ) && empty( $_POST['billing_account_number'] ) ) {
        $errors->add( 'billing_account_number_error', __( '<strong>Error</strong>: account number is required!', 'woocommerce' ) );
        $fields['billing_account_number']['maxlength'] = 6;
    }
    return $errors;
}

// 3. SAVE FIELDS
add_action( 'woocommerce_created_customer', 'save_woo_account_fields' );

function save_woo_account_fields( $customer_id ) {
    if ( isset( $_POST['billing_account_number'] ) ) {
        update_user_meta( $customer_id, 'billing_account_number', sanitize_text_field( $_POST['billing_account_number'] ) );
        update_user_meta( $customer_id, 'account_number', sanitize_text_field($_POST['billing_account_number']) );
    }
}

但是字段验证不起作用,因为用户仍然可以在不填写的情况下进行注册.
我做错了什么?

But the field validation doesn't work as the user still can register without filling it.
What I am doing wrong?

此外,当客户注册时,我希望在管理员用户页面上看到此字段.

Also when customer register I would like to see this field on the admin user page.

感谢任何帮助.

推荐答案

以下会在客户注册中添加一个账单账号"字段作为必填字段(验证),并且也会显示出来:

The following will add a "billing account number" field in customer registration as a required field (validation) and will also display it:

  • 在我的帐户 > 编辑帐户部分
  • 在帐单字段部分的管理员用户编辑页面中

代码:

// Display a field in Registration / Edit account
add_action( 'woocommerce_register_form_start', 'display_account_registration_field' );
add_action( 'woocommerce_edit_account_form_start', 'display_account_registration_field' );
function display_account_registration_field() {
    $user  = wp_get_current_user();
    $value = isset($_POST['billing_account_number']) ? esc_attr($_POST['billing_account_number']) : $user->billing_account_number;
    ?>
    <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
    <label for="reg_billing_account_number"><?php _e( 'Ship to/ Account number', 'woocommerce' ); ?> <span class="required">*</span></label>
    <input type="text" maxlength="6" class="input-text" name="billing_account_number" id="reg_billing_account_number" value="<?php echo $value ?>" />
    </p>
    <div class="clear"></div>
    <?php
}

// registration Field validation
add_filter( 'woocommerce_registration_errors', 'account_registration_field_validation', 10, 3 );
function account_registration_field_validation( $errors, $username, $email ) {
    if ( isset( $_POST['billing_account_number'] ) && empty( $_POST['billing_account_number'] ) ) {
        $errors->add( 'billing_account_number_error', __( '<strong>Error</strong>: account number is required!', 'woocommerce' ) );
    }
    return $errors;
}

// Save registration Field value
add_action( 'woocommerce_created_customer', 'save_account_registration_field' );
function save_account_registration_field( $customer_id ) {
    if ( isset( $_POST['billing_account_number'] ) ) {
        update_user_meta( $customer_id, 'billing_account_number', sanitize_text_field( $_POST['billing_account_number'] ) );
    }
}

// Save Field value in Edit account
add_action( 'woocommerce_save_account_details', 'save_my_account_billing_account_number', 10, 1 );
function save_my_account_billing_account_number( $user_id ) {
    if( isset( $_POST['billing_account_number'] ) )
        update_user_meta( $user_id, 'billing_account_number', sanitize_text_field( $_POST['billing_account_number'] ) );
}

// Display field in admin user billing fields section
add_filter( 'woocommerce_customer_meta_fields', 'admin_user_custom_billing_field', 10, 1 );
function admin_user_custom_billing_field( $args ) {
    $args['billing']['fields']['billing_account_number'] = array(
        'label'         => __( 'Ship to/ Account number', 'woocommerce' ),
        'description'   => '',
        'custom_attributes'   => array('maxlength' => 6),
    );
    return $args;
}

代码位于您的活动子主题(活动主题)的 function.php 文件中.经测试有效.

Code goes in function.php file of your active child theme (active theme). Tested and works.

这篇关于在 Woocommerce 注册表和管理员编辑用户中添加一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆