WooCommerce 教程:在用户注册和我的账户页面添加自定义字段

2018-12-2210:18:35网站建设与开发Comments4,154 views字数 2101阅读模式

多数情况下,在电子商务网站中,我们都希望在用户注册时就获取更多的用户信息,而不仅仅是用户名和密码。WooCommerce 可以自定义功能页面的模版文件,这就给我们自定义用户注册页面和我的账户页面的字段提供了可能。把相应的模板文件复制到主题文件夹中相应的位置。然后我们就可以开始自定义开发 WooCommerce 模版文件了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/9006.html

编辑主题文件夹中,WooCommerce 的form-login.php文件,比如我要添加用户的姓名到注册页面。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/9006.html

首先,我们需要添加自定义用户注册表单

<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>

然后,在用户注册时保存这些数据

在 functions.php 中,挂载保存用户的功能到 “user_register” 动作。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/9006.html

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']);
}

最后,编辑用户页面也要添加同样的数据

用户编辑页面的代码在 woocommerce/myaccount/form-edit-account.php中,和 form-login.php一样,添加相应的字段即可。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/9006.html

<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>

虽然在技术上,注册时获取更多的用户信息是可行的,但是会不会增加注册的难度,导致用户注册率的降低?很多用户对个人隐私都比较敏感,特别是在缺乏信任的互联网上,获取更多的用户信息之前,先仔细考虑一下,没有这些信息是否不行?如果没有这些信息,用户交易可以照常进行,还是不要这些信息比较好。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/9006.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/cms/9006.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定