以下代碼是在註冊頁面插入【姓氏】、【名字】、【電話】,我自己有調整過後的版本

如果要添加其他欄位並將這些註冊表單字段與賬單地址相關聯,您必須在字段名稱之前添加前綴“ billing_”。

以下是所有有效的WooCommerce表單字段的列表,這些字段可以添加到註冊表單中,並且可以與帳單地址相關聯。

  • billing_first_name
  • billing_last_name
  • billing_company
  • billing_address_1
  • billing_address_2
  • billing_city
  • billing_postcode
  • billing_country
  • billing_state
  • billing_email
  • billing_phone

以下是添加欄位的位置

  • woocommerce_register_form_start : 在表單的開頭
  • woocommerce_register_form : 在註冊按鈕之前

參考網站

註冊頁面新增欄位-手機/姓氏/名字

//新增註冊欄位
function wooc_extra_register_fields() {?>
		<p class="form-row form-row-wide">
		<label for="reg_billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?><span class="required">*</span></label>
		<input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php esc_attr_e( $_POST['billing_phone'] ); ?>" />
		</p>
		
		<p class="form-row form-row-first">
		<label for="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?><span class="required">*</span></label>
		<input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
		</p>
		
		<p class="form-row form-row-last">
		<label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?><span class="required">*</span></label>
		<input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
		</p>
		
		<div class="clear"></div>
		<?php
 }
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );

/**
* 註冊字段驗證必填
*/
function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
		if ( isset( $_POST['billing_phone'] ) && empty( $_POST['billing_phone'] ) ) {
			$validation_errors->add( 'billing_phone_error', __( '聯絡電話為<strong>必填</strong>', 'woocommerce' ) );
		}

		if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
			$validation_errors->add( 'billing_last_name_error', __( '姓氏為<strong>必填</strong>', 'woocommerce' ) );
		}

		if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
			$validation_errors->add( 'billing_first_name_error', __( '名字為<strong>必填</strong>', 'woocommerce' ) );
		}
			return $validation_errors;
}
add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );

/**
* 保存字段於資料庫
*/
function wooc_save_extra_register_fields( $customer_id ) {
	if ( isset( $_POST['billing_phone'] ) ) {
		// Phone input filed which is used in WooCommerce
		update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
	}
	if ( isset( $_POST['billing_first_name'] ) ) {
		//First name field which is by default
		update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
		// First name field which is used in WooCommerce
		update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
	}
	if ( isset( $_POST['billing_last_name'] ) ) {
		// Last name field which is by default
		update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
		// Last name field which is used in WooCommerce
		update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
	}
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );

註冊頁面新增欄位-手機 (第一種)

//新增註冊欄位
function wooc_extra_register_fields() {?>
        <p class="form-row form-row-wide">
        <label for="reg_billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php esc_attr_e( $_POST['billing_phone'] ); ?>" />
        </p>
        
        <div class="clear"></div>
        <?php
 }
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );

/**
* 註冊字段驗證必填
*/
function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
        if ( isset( $_POST['billing_phone'] ) && empty( $_POST['billing_phone'] ) ) {
            $validation_errors->add( 'billing_phone_error', __( '聯絡電話為<strong>必填</strong>', 'woocommerce' ) );
        }

            return $validation_errors;
}
add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );

/**
* 保存字段於資料庫
*/
function wooc_save_extra_register_fields( $customer_id ) {
    if ( isset( $_POST['billing_phone'] ) ) {
        // Phone input filed which is used in WooCommerce
        update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
    }

}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );

註冊頁面新增欄位-姓名(first name)+地址

//新增註冊欄位
function wooc_extra_register_fields_before() {?>
	<p class="form-row form-row-wide">
	<label for="reg_billing_first_name"><?php _e( '姓名', 'woocommerce' ); ?><span class="required">*</span></label>
	<input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
	</p>
	
	<div class="clear"></div>
	<?php
 }
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields_before' );


function wooc_extra_register_fields_after() {?>
	<p class="form-row form-row-first">
	<label for="reg_billing_state"><?php _e( '縣/市', 'woocommerce' ); ?><span class="required">*</span></label>
	<input type="text" class="input-text" name="billing_state" id="reg_billing_state" value="<?php if ( ! empty( $_POST['billing_state'] ) ) esc_attr_e( $_POST['billing_state'] ); ?>" />
	</p>

	<p class="form-row form-row-last">
	<label for="reg_billing_city"><?php _e( '鄉鎮市', 'woocommerce' ); ?><span class="required">*</span></label>
	<input type="text" class="input-text" name="billing_city" id="reg_billing_city" value="<?php if ( ! empty( $_POST['billing_city'] ) ) esc_attr_e( $_POST['billing_city'] ); ?>" />
	</p>

	<p class="form-row form-row-first">
	<label for="reg_billing_postcode"><?php _e( '郵遞區號', 'woocommerce' ); ?><span class="required">*</span></label>
	<input type="text" class="input-text" name="billing_postcode" id="reg_billing_postcode" value="<?php if ( ! empty( $_POST['billing_postcode'] ) ) esc_attr_e( $_POST['billing_postcode'] ); ?>" />
	</p>

	<p class="form-row form-row-last">
	<label for="reg_billing_address_1"><?php _e( '地址', 'woocommerce' ); ?><span class="required">*</span></label>
	<input type="text" class="input-text" name="billing_address_1" id="reg_billing_address_1" value="<?php if ( ! empty( $_POST['billing_address_1'] ) ) esc_attr_e( $_POST['billing_address_1'] ); ?>" />
	</p>
	
	<div class="clear"></div>
	<?php
 }
add_action( 'woocommerce_register_form', 'wooc_extra_register_fields_after' );



/**
* 註冊字段驗證必填
*/
function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
	if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
		$validation_errors->add( 'billing_first_name_error', __( '姓名為<strong>必填</strong>', 'woocommerce' ) );
	}
		return $validation_errors;
}
add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );

/**
* 保存字段於資料庫
*/
function wooc_save_extra_register_fields( $customer_id ) {
	if ( isset( $_POST['billing_first_name'] ) ) {
		// Last name field which is by default
		update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_first_name'] ) );
		// Last name field which is used in WooCommerce
		update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
	}
	
	if ( isset( $_POST['billing_state'] ) ) {
		// state field which is by default
		update_user_meta( $customer_id, 'state', sanitize_text_field( $_POST['billing_state'] ) );
		// state field which is used in WooCommerce
		update_user_meta( $customer_id, 'billing_state', sanitize_text_field( $_POST['billing_state'] ) );
	}

	if ( isset( $_POST['billing_city'] ) ) {
		// city field which is by default
		update_user_meta( $customer_id, 'city', sanitize_text_field( $_POST['billing_city'] ) );
		// city field which is used in WooCommerce
		update_user_meta( $customer_id, 'billing_city', sanitize_text_field( $_POST['billing_city'] ) );
	}

	if ( isset( $_POST['billing_postcode'] ) ) {
		// postcode field which is by default
		update_user_meta( $customer_id, 'postcode', sanitize_text_field( $_POST['billing_postcode'] ) );
		// postcode field which is used in WooCommerce
		update_user_meta( $customer_id, 'billing_postcode', sanitize_text_field( $_POST['billing_postcode'] ) );
	}

	if ( isset( $_POST['billing_address_1'] ) ) {
		// address_1 field which is by default
		update_user_meta( $customer_id, 'address_1', sanitize_text_field( $_POST['billing_address_1'] ) );
		// address_1 field which is used in WooCommerce
		update_user_meta( $customer_id, 'billing_address_1', sanitize_text_field( $_POST['billing_address_1'] ) );
	}
	
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );

註冊頁面新增欄位-手機 (第二種)

參考資料:https://rudrastyh.com/woocommerce/add-registration-form-field.html

//woo 註冊表單 新增額外的選項 (電話)
add_action( 'woocommerce_register_form', 'misha_add_register_form_field' );
function misha_add_register_form_field(){
  
  woocommerce_form_field(
    'phone_number',
    array(
      'type'        => 'tel',
      'required'    => true, // just adds an "*"
      'label'       => '電話號碼'
    ),
    ( isset( $_POST[ 'phone_number' ] ) ? $_POST[ 'phone_number' ] : '' )
  );
}

  woocommerce_form_field(
    'country_to_visit',
    array(
      'type'        => 'text',
      'required'    => false, // 是否為避填 "*"
      'label'       => '備註'
    ),
    ( isset($_POST['country_to_visit']) ? $_POST['country_to_visit'] : '' )
  );


//保存到database
add_action( 'woocommerce_created_customer', 'misha_save_register_fields' );
function misha_save_register_fields( $customer_id ){
  
  if ( isset( $_POST[ 'phone_number' ] ) ) {
    update_user_meta( $customer_id, 'phone_number', wc_clean( $_POST[ 'phone_number' ] ) );
  }
  
}

我的帳戶字段編輯

參考資料:https://rudrastyh.com/woocommerce/edit-account-fields.html

最後修改日期: 2022 年 7 月 19 日

作者