Forcing Customer To Choose WooCommerce Role Upon Registration - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Wednesday, October 4, 2023

Forcing Customer To Choose WooCommerce Role Upon Registration

I have read about this and seen others asking the same thing, but no matter what I do or did, nothing seem to work the way I need it.

I have already removed all other roles (editor, subscriber, contributer, shop manager etc.) and I now only have Administrator and Customer left.

I therefore, need the all new users, when filling out the WooCommerce registration form, to choose between "Buyer" and "Seller".

This is the code I am working with:

add_action( 'woocommerce_register_form', 'user_role_dropdown' );
function user_role_dropdown() {

    $wp_roles = new WP_Roles();
    $wp_roles->use_db = true;
    echo '<select id="role" name="role">';
    
        foreach ( $wp_roles->roles as $key=>$value ) {
        if ( ( $value['name'] !== 'Buyer') && ( $value['name'] !== 'Seller' ) ) {
    
        echo '<option value="' . $key . '">' . $value['name'] . '</option>';
        }
    }
    echo '</select>';
}

add_action( 'personal_options_update', 'save_role_selection_field' );
add_action( 'edit_user_profile_update', 'save_role_selection_field' );
function save_role_selection_field( $user_id ) {

    update_user_meta( $user_id, 'role', $_POST['role'] );
    $user = new WP_User( $user_id );
    $current_user_role = get_current_user_role();
    $user->remove_role( $current_user_role );
    $user->add_role( $_POST['role'] );
}

function get_current_user_role() {
    global $current_user;
    get_currentuserinfo();
    $user_roles = $current_user->roles;
    $user_role = array_shift( $user_roles );
    return $user_role;
};


add_action( 'user_register', 'register_role' );
function register_role( $user_id, $password="", $meta=array() ) {

    $userdata = array();
    $userdata[ 'ID' ] = $user_id;
    $userdata[ 'role' ] = $_POST[ 'role' ];
    if ( $userdata[ 'role' ] ) {
    wp_update_user( $userdata );
    }
}

add_filter( 'registration_errors', 'new_usersroles_registration_errors', 10, 3 );
function new_usersroles_registration_errors( $errors, $sanitized_user_login, $user_email ) {

    if ( empty( $_POST['role'] ) || ! empty( $_POST['role'] ) && trim( $_POST['role'] ) == '' ) {

        $errors->add( 'role_error', __( '<strong>FYI:</strong> You must choose a role for your account. Buyers can not sell. Sellers cannot buy.', 'woocommerce' ) );
    }

    return $errors;
}

add_action( 'user_register', 'new_usersroles_user_register' );
function new_usersroles_user_register( $user_id ) {
$user_id = wp_update_user( array( 'ID' => $user_id, 'role' => $_POST[ 'role' ] ) );
}


source https://stackoverflow.com/questions/77224294/forcing-customer-to-choose-woocommerce-role-upon-registration

No comments:

Post a Comment