복붙노트

[WORDPRESS] Woocommerce 등록에 사용자 역할을 선택 작동이 중지

WORDPRESS

Woocommerce 등록에 사용자 역할을 선택 작동이 중지

해결법


  1. 1.나는 이전 코드를 폐기하기로 결정하고 다음 코드로 대체 그것은 작동합니다.

    나는 이전 코드를 폐기하기로 결정하고 다음 코드로 대체 그것은 작동합니다.

    /* 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');
    
  2. from https://stackoverflow.com/questions/44607961/user-role-select-on-woocommerce-registration-stopped-working by cc-by-sa and MIT license