1. Home
  2. Docs
  3. Woocommerce Basic
  4. 마이어카운트 – 회원가입 필드 추가

마이어카운트 – 회원가입 필드 추가

플러그인 없이 코드로 필드를 추가하는 방법

// !!! 가입폼과, 계정수정 폼의 필드 표시는 서로 다름. 왜냐하면 일부 에러가 난 필드 제외하고는 정보를 계속 유지할 수 있게 해야 하므로 
// 우커머스 탬플릿 - form-edit-account.php의 woocommerce_edit_account_form 훅 위치를 비번 변경 위로 옮김 

// To My-account > edit account --------------------------------------------------------
/**
 * Step 1. Add your field
 */
add_action( 'woocommerce_edit_account_form', 'custom_add_field_edit_account_form' );
// or add_action( 'woocommerce_edit_account_form_start', 'misha_add_field_edit_account_form' );
function custom_add_field_edit_account_form() { 
    woocommerce_form_field(
        'custom_user_info_field',
        array(
            'type'        => 'text',
            'required'    => true, // remember, this doesn't make the field required, just adds an "*"
            'label'       => '계정 추가 정보 입력(사이트마다 필요한 추가 정보들)',
        //	'description' => '(가입 폼에도 나오고, 계정 수정에서도 편집이 가능해야 하는 필드)',
        ),
        get_user_meta( get_current_user_id(), 'custom_user_info_field', true ) // get the data
    ); 
} 
/**
 * Step 2. Save field value
 */
add_action( 'woocommerce_save_account_details', 'custom_save_account_details' );
function custom_save_account_details( $user_id ) { 
    update_user_meta( $user_id, 'custom_user_info_field', sanitize_text_field( $_POST['custom_user_info_field'] ) ); 
} 
/**
 * Step 3. Make it required
 */
add_filter('woocommerce_save_account_details_required_fields', 'custom_make_field_required');
function custom_make_field_required( $required_fields ){ 
    $required_fields['custom_user_info_field'] = '계정 추가 정보 입력(사이트마다 필요한 추가 정보들)';
    return $required_fields; 
}


// To My-account > regist -------------------------------------------------------------------------
/**
 * Step 1. Add your field
 */
add_action( 'woocommerce_register_form', 'custom_add_register_form_field' );
function custom_add_register_form_field(){ 
    woocommerce_form_field(
        'custom_user_info_field',
        array(
            'type'        => 'text',
            'required'    => true, // just adds an "*"
            'label'       => '계정 추가 정보 입력(사이트마다 필요한 추가 정보들)'
        ),
        ( isset($_POST['custom_user_info_field']) ? $_POST['custom_user_info_field'] : '' )  // 폼에 입력된 정보 유지!!! 
    ); 
}
/**
 * Step 2. Validation Fields
 */
add_action( 'woocommerce_register_post', 'custom_validate_registration_fields', 10, 3 ); 
function custom_validate_registration_fields( $username, $email, $errors ) { 
    if ( empty( $_POST['custom_user_info_field'] ) ) {
        $errors->add( 'custom_user_info_field_error', '안내될 에러 메시지' );
    } 
}
/**
 * Step 3. save the field data into the database
 */
add_action( 'woocommerce_created_customer', 'custom_save_register_fields' ); 
function custom_save_register_fields( $customer_id ){ 
    if ( isset( $_POST['custom_user_info_field'] ) ) {
        update_user_meta( $customer_id, 'custom_user_info_field', wc_clean( $_POST['custom_user_info_field'] ) );
    }   // wc_clean() is kind of WooCommerce version of sanitize_text_field()
}