User Role Select on Woocommerce Registration Stopped Working

前端 未结 1 501
你的背包
你的背包 2021-01-17 03:40

I use a drop down menu to select between two user roles on my Woocommerce registration form. After updating to Woocommerce 3.0.8 the drop down menu stopped working and I can

相关标签:
1条回答
  • 2021-01-17 04:02

    I decided to scrap the old code and replaced it with the following code and it works now.

    /* To add WooCommerce registration form custom fields. */
    
    function WC_extra_registation_fields() {?>
    <p class="form-row form-row-first">
        <label for="reg_role"><?php _e( 'Dealer or Distributor', 'woocommerce' ); ?></label>
        <select class="input-text" name="role" id="reg_role"> 
        <option <?php if ( ! empty( $_POST['role'] ) && $_POST['role'] == 'dealer') esc_attr_e( 'selected' ); ?> value="dealer">Dealer</option> 
        <option <?php if ( ! empty( $_POST['role'] ) && $_POST['role'] == 'distributor') esc_attr_e( 'selected' ); ?> value="distributor">Distributor</option>
        </select> 
    </p>
    
    <?php
    }
    
    add_action( 'woocommerce_register_form', 'WC_extra_registation_fields');
    
    
    /* To validate WooCommerce registration form custom fields.  */
    function WC_validate_reg_form_fields($username, $email, $validation_errors) {
    if (isset($_POST['role']) && empty($_POST['role']) ) {
        $validation_errors->add('role_error', __('Dealer or Distributor is required!', 'woocommerce'));
    }
    
    return $validation_errors;
    }
    
    add_action('woocommerce_register_post', 'WC_validate_reg_form_fields', 10, 3);
    
    
    /* To save WooCommerce registration form custom fields. */
    function WC_save_registration_form_fields($customer_id) {
    
    //Role field
    if (isset($_POST['role'])) {
        update_user_meta($customer_id, 'role', sanitize_text_field($_POST['role']));
    }
    
    }
    
    add_action('woocommerce_created_customer', 'WC_save_registration_form_fields');
    
    0 讨论(0)
提交回复
热议问题