
本教程详细介绍了如何在woocommerce的“我的账户”注册表单中添加自定义生日字段,并解决数据无法正确保存的问题。文章通过修正月份下拉菜单的value属性和优化生日数据保存逻辑,确保用户输入的生日信息能以yyyy-mm-dd格式成功存储到用户元数据中,从而完善用户注册体验。
在WooCommerce商店中,有时我们需要收集更多用户注册信息,例如生日。通过在“我的账户”注册表单中添加自定义字段,可以实现这一目标。本教程将指导您如何添加一个由日、月、年三个下拉菜单组成的生日字段,并确保其数据能够正确保存。
原始代码在添加生日字段时,使用了三个select标签分别代表日、月、年。主要问题出在以下两个方面:
要解决上述问题,我们需要对月份下拉菜单的value属性和数据保存逻辑进行修正。
首先,我们需要使用woocommerce_register_form_start钩子在注册表单中插入自定义字段。关键在于为月份select标签的每个选项设置正确的数字value属性。
/**
* 添加自定义字段到WooCommerce“我的账户”注册表单
*/
add_action( 'woocommerce_register_form_start', function() {
?>
<p class="form-row form-row-first-name">
<label for="billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_first_name" id="billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
</p>
<p class="form-row form-row-last-name">
<label for="billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_last_name" id="billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
</p>
<table class="form-table">
<tr>
<th><label for="birth-date-day"><?php echo __( 'Birthday' ); ?></label></th>
<td>
<select id="birth-date-day" name="birthday[day]">
<option selected="selected" value=""><?php echo __( 'Day' ); ?></option><?php
for( $i = 1; $i <= 31; $i++ ) {
printf( '<option value="%1$s">%1$s</option>', $i );
} ?>
</select>
<select id="birth-date-month" name="birthday[month]">
<option selected="selected" value=""><?php echo __( 'Month' ); ?></option>
<option value="1"><?php _e( 'January' ); ?></option>
<option value="2"><?php _e( 'February' ); ?></option>
<option value="3"><?php _e( 'March' ); ?></option>
<option value="4"><?php _e( 'April' ); ?></option>
<option value="5"><?php _e( 'May' ); ?></option>
<option value="6"><?php _e( 'June' ); ?></option>
<option value="7"><?php _e( 'July' ); ?></option>
<option value="8"><?php _e( 'August' ); ?></option>
<option value="9"><?php _e( 'September' ); ?></option>
<option value="10"><?php _e( 'October' ); ?></option>
<option value="11"><?php _e( 'November' ); ?></option>
<option value="12"><?php _e( 'December' ); ?></option>
</select>
<select id="birth-date-year" name="birthday[year]">
<option selected="selected" value=""><?php echo __( 'Year' ); ?></option><?php
for( $i = date("Y") - 14; $i >= date("Y") - 99 ; $i-- ) {
printf( '<option value="%1$s">%1$s</option>', $i );
} ?>
</select>
</td>
</tr>
</table>
<?php
} );关键修正点:
为了确保用户提供了必要的生日信息,我们需要在注册提交时进行验证。使用woocommerce_register_post钩子可以添加自定义验证规则。
/**
* 验证“我的账户”注册表单自定义字段
*/
add_action( 'woocommerce_register_post', function( $username, $email, $validation_errors ) {
if( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
$validation_errors->add( 'billing_first_name_error', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) );
}
if( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$validation_errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Last name is required!.', 'woocommerce' ) );
}
// 验证生日字段是否完整填写
if( isset( $_POST['birthday'] ) ) {
$birthday_data = $_POST['birthday'];
if ( empty( $birthday_data['day'] ) || empty( $birthday_data['month'] ) || empty( $birthday_data['year'] ) ) {
$validation_errors->add( 'birthday_error', __( '<strong>Error</strong>: Birthday is required!.', 'woocommerce' ) );
}
} else {
$validation_errors->add( 'birthday_error', __( '<strong>Error</strong>: Birthday is required!.', 'woocommerce' ) );
}
return $validation_errors;
}, 10, 3 );关键修正点:
最后,使用woocommerce_created_customer钩子在用户注册成功后保存生日数据。这里我们将提交的日、月、年数组组合成一个YYYY-MM-DD格式的日期字符串,并存储为用户元数据。
/**
* 保存“我的账户”注册表单额外字段
*/
add_action( 'woocommerce_created_customer', function( $customer_id ) {
if( isset( $_POST['billing_first_name'] ) ) {
update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
}
if( isset( $_POST['billing_last_name'] ) ) {
update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
}
if( isset( $_POST['birthday'] ) ) {
$birthday_data = array_map( 'sanitize_text_field', $_POST['birthday'] ); // 对数组元素进行安全过滤
// 将日、月、年数组转换为“年-月-日”格式的字符串
// 例如:array('day' => '15', 'month' => '7', 'year' => '1990') -> "1990-7-15"
$birthday_string = $birthday_data['year'] . '-' . $birthday_data['month'] . '-' . $birthday_data['day'];
// 使用strtotime解析并格式化为 YYYY-MM-DD
$birthday = date( 'Y-m-d', strtotime( $birthday_string ) );
update_user_meta( $customer_id, 'birthday', $birthday );
}
} );关键修正点:
将以上所有代码片段合并,放入您主题的functions.php文件或自定义插件中。
登录后复制
通过遵循本教程的步骤和修正,您将能够成功地在WooCommerce注册表单中添加自定义生日字段,并确保用户数据能够被准确、安全地保存。
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号