大多數(shù)情況下,在電子商務(wù)網(wǎng)站中,我們都希望在用戶注冊時就獲取更多的用戶信息,而不僅僅是用戶名和密碼。WooCommerce可以自定義功能頁面的模版文件,這就給我們自定義用戶注冊頁面和我的賬戶頁面的字段提供了可能。把相應(yīng)的模板文件復(fù)制到主題文件夾中相應(yīng)的位置。然后我們就可以開始自定義開發(fā)WooCommerce模版文件了。
編輯主題文件夾中,WooCommerce的form-login.php文件,比如我要添加用戶的姓名到注冊頁面。
首先,我們需要添加自定義用戶注冊表單
<p class="form-row form-row-wide">
<label for="reg_first_name"><?php _e( 'First Name', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" required name="first_name" id="reg_first_name" value="<?php if ( ! empty( $_POST['first_name'] ) ) echo esc_attr( $_POST['first_name'] ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="reg_last_name"><?php _e( 'Last Name', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" required name="last_name" id="reg_last_name" value="<?php if ( ! empty( $_POST['last_name'] ) ) echo esc_attr( $_POST['last_name'] ); ?>" />
</p>
然后,在用戶注冊時保存這些數(shù)據(jù)
在 functions.php 中,掛載保存用戶的功能到 “user_register” 動作。
add_action( 'user_register', 'pft_registration_save', 10, 1 );
function pft_registration_save( $user_id ) {
if ( isset( $_POST['first_name'] ) )
update_user_meta($user_id, 'first_name', $_POST['first_name']);
if ( isset( $_POST['last_name'] ) )
update_user_meta($user_id, 'last_name', $_POST['last_name']);
if ( isset( $_POST['title'] ) )
update_user_meta($user_id, 'title', $_POST['title']);
if ( isset( $_POST['dob'] ) )
update_user_meta($user_id, 'dob', $_POST['dob']);
}
最后,編輯用戶頁面也要添加同樣的數(shù)據(jù)
用戶編輯頁面的代碼在 woocommerce/myaccount/form-edit-account.php中,和 form-login.php一樣,添加相應(yīng)的字段即可。
<p class="form-row form-row-first">
<label for="account_first_name"><?php _e( 'First name', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="account_first_name" id="account_first_name" value="<?php echo esc_attr( $user->first_name ); ?>" />
</p>
<p class="form-row form-row-last">
<label for="account_last_name"><?php _e( 'Last name', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="account_last_name" id="account_last_name" value="<?php echo esc_attr( $user->last_name ); ?>" />
</p>
雖然在技術(shù)上,注冊時獲取更多的用戶信息是可行的,但是會不會增加注冊的難度,導(dǎo)致用戶注冊率的降低?很多用戶對個人隱私都比較敏感,特別是在缺乏信任的互聯(lián)網(wǎng)上,獲取更多的用戶信息之前,先仔細(xì)考慮一下,沒有這些信息是否不行?如果沒有這些信息,用戶交易可以照常進(jìn)行,還是不要這些信息比較好。



你好,請教如何把wordpress用戶管理中的 昵稱和公開顯示為 這兩項也添加到 woocommerce注冊選項中 ,