我创建了一个插件,可以使用 WooCommerce 中的其他字段扩展帐户详细信息。由于这些字段需要为必填字段,因此我已使用
woocommerce_save_account_details_required_fields
将它们设为必填字段,这样就可以了。 这是我的代码:
add_action( 'woocommerce_edit_account_form', 'andom_add_id_field_to_edit_account_form' );
function andom_add_id_field_to_edit_account_form() {
$user = wp_get_current_user();
?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="id_card"><?php _e( 'Id Card ', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="id_card" id="id_card" value="<?php echo esc_attr( $user->id_card ); ?>" required />
</p>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="adults"><?php _e( 'Adults in your household ', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="adults" id="adults" value="<?php echo esc_attr( $user->adults ); ?>" required/>
</p>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="children"><?php _e( 'Children >= 18 in your household ', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="children" id="children" value="<?php echo esc_attr( $user->children ); ?>" required/>
</p>
<?php
}
add_filter( 'woocommerce_save_account_details_required_fields', 'andom_make_field_required' );
function andom_make_field_required( $required_fields ){
$required_fields[ 'id_card' ] = 'Id Card';
$required_fields[ 'adults' ] = 'Adults';
$required_fields[ 'children' ] = 'Children';
return $required_fields;
}
正如我所说,到目前为止它是有效的,唯一不行的是 0 值似乎被认为是无意义的。
对于您所需的附加字段,请使用
woocommerce_save_account_details_errors
挂钩,而不是不允许 woocommerce_save_account_details_required_fields
值的 0
。
您的代码也可以被优化和压缩。
尝试以下完整代码(替换所有现有的相关代码):
// Utility function: Your additional fields settings
function edit_account_additional_fields() {
return array (
'id_card' => __( 'Id Card', 'woocommerce' ),
'adults' => __( 'Adults in your household', 'woocommerce' ),
'children' => __( 'Children >= 18 in your household', 'woocommerce' ),
);
}
// Display custom additional fields
add_action( 'woocommerce_edit_account_form', 'edit_account_form_additional_fields_display' );
function edit_account_form_additional_fields_display() {
foreach ( edit_account_additional_fields() as $key => $label ) {
printf('<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="%s">%s <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="%s" id="%s" value="%s" required />
</p>', $key, $label, $key, $key, get_user_meta( get_current_user_id(), $key, true ) );
}
}
// custom additional fields validation
add_action( 'woocommerce_save_account_details_errors', 'edit_account_form_required_fields_validation', 10, 2 );
function edit_account_form_required_fields_validation( $errors, $user ) {
foreach ( edit_account_additional_fields() as $key => $label ) {
if( isset($_POST[$key]) && $_POST[$key] == '' ) {
$errors->add( 'error', sprintf( __('%s is a required field.', 'woocommerce'), '<strong>'.esc_html($label).'</strong>' ),'');
}
}
}
// Save the custom additional fields values
add_action( 'woocommerce_save_account_details', 'edit_account_form_save_additional_fields' );
function edit_account_form_save_additional_fields( $user_id ) {
foreach ( edit_account_additional_fields() as $key => $label ) {
if( isset($_POST[$key]) && $_POST[$key] != '' ) {
update_user_meta( $user_id, $key , sanitize_text_field($_POST[$key]) );
}
}
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。