【问题标题】:Add radio buttons with validation on My account > edit account in WooCommerce在我的帐户上添加带有验证的单选按钮 > 在 WooCommerce 中编辑帐户
【发布时间】: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', '' ); ?>&nbsp;<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" />&nbsp;&nbsp;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" />&nbsp;&nbsp;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" />&nbsp;&nbsp;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


    【解决方案1】:

    您的代码包含一些错误/错误

    • &lt;div&gt; 标记不能在&lt;p&gt; 标记内,因为段落将在输入&lt;div&gt; 标记的位置断开。
    • 所有输入所需的设置更加清晰,但不是必需的(除非动态生成单选按钮)。
    • 您在ID 上有错字,由于缺少",它无法正常关闭
    • user_profile_update_errors 钩子在这里不适用,请改用woocommerce_save_account_details_errors
    • 虽然可以添加用于创建单选按钮的代码,但为了清楚起见,我通过修改模板文件将其添加到我的答案中。

    所以你得到:

    // Add field
    function action_woocommerce_edit_account_form_start() {
        // Retrieve the current user object.
        $user = wp_get_current_user();
        ?>
        <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
            <label for="gender"><?php esc_html_e( 'Gender', 'woocommerce' ); ?>&nbsp;<span class="required">*</span></label>
            <input type="radio" class="radio" name="gender" value="Male" <?php if( 'Male' == esc_attr( get_the_author_meta( 'gender', $user->ID ) ) ) echo 'checked'; ?>>&nbsp;&nbsp;Male<br /> 
            <input type="radio" class="radio" name="gender" value="Female" <?php if( 'Female' == esc_attr( get_the_author_meta( 'gender', $user->ID ) ) ) echo 'checked'; ?>>&nbsp;&nbsp;Female<br /> 
            <input type="radio" class="radio" name="gender" value="Others" <?php if ( 'Others' == esc_attr( get_the_author_meta( 'gender', $user->ID ) ) ) echo 'checked'; ?>>&nbsp;&nbsp;Others 
        </p>
        <?php
    }
    add_action( 'woocommerce_edit_account_form_start', 'action_woocommerce_edit_account_form_start' );
    
    // Validate - my account
    function action_woocommerce_save_account_details_errors( $args ) {
        if ( ! isset( $_POST['gender'] ) ) {
            $args->add( 'error', '<strong>' . __( 'Gender', 'woocommerce' ) . '</strong> ' . __( 'is a required field.', 'woocommerce' ) );
        }
    }
    add_action( 'woocommerce_save_account_details_errors', 'action_woocommerce_save_account_details_errors', 10, 1 );
    
    // Save - my account
    function action_woocommerce_save_account_details( $user_id ) {
        if ( isset( $_POST['gender'] ) ) {
            // Update field
            update_user_meta( $user_id, 'gender', sanitize_text_field( $_POST['gender'] ) );
        }
    }
    add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );
    

    【讨论】:

      猜你喜欢
      • 2018-12-08
      • 1970-01-01
      • 2020-09-12
      • 2022-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-08
      • 1970-01-01
      相关资源
      最近更新 更多