【发布时间】:2021-07-13 06:47:56
【问题描述】:
通过下面的代码,我在 WooCommerce 中我的帐户的个人资料页面上添加了单选按钮。
问题是我无法使单选按钮成为必需且无法显示通知 当没有选择时。
这是我的代码...
HTML
// Add & display radio
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<div class="myAccount_radiobtn">
<label for="gender"><?php esc_html_e( 'Gender', '' ); ?> <span class="required">*</span></label>
<div class="my_account_profile_radio">
<input type="radio" required name="gender" id="gender value="Male" <?php if('Male'==esc_attr(get_the_author_meta('gender',$user->ID ))) echo 'checked="checked"'; ?> class="radio" /> Male<br />
<input type="radio" required name="gender" id="gender value="Female" <?php if('Female'==esc_attr(get_the_author_meta('gender',$user->ID ))) echo 'checked="checked"'; ?> class="radio" /> Female<br />
<input type="radio" required name="gender" id="gender value="Others" <?php if ('Others'==esc_attr(get_the_author_meta('gender',$user->ID ))) echo 'checked="checked"'; ?> class="radio" /> Others
</div>
</div>
</p>
PHP
// Save radio
function save_user_account_details( $user_id ) {
if( isset( $_POST['gender'] ) )
update_user_meta( $user_id, 'gender', sanitize_text_field( $_POST['gender'] ) );
}
add_action( 'woocommerce_save_account_details', 'save_user_account_details', 12, 1 );
// Validation radio
function myaccount_fields_validation2 ( $errors ) {
if ( isset( $_POST['gender'] ) ) {
if(strlen($_POST['gender'])<4 )
$errors->add( 'error', __( '<strong>Gender</strong> is a required field.', '' ),'');
}
}
add_action( 'user_profile_update_errors','myaccount_fields_validation2', 10, 1 );
有什么建议吗?
【问题讨论】:
标签: php wordpress woocommerce custom-fields account