Okay
  Public Ticket #3615922
to use registration role for specific redirection
Closed

Comments

  • Jose started the conversation

    Hello, 

    I want to use registration role for specific redirection. I've seen that I can get registration role of current user using b2bking_registration_role key

    But it returns only a key (role_25803215). I need to have registration role label. How can I do that ? Thx

  •  1,910
    WebWizards replied

    Hello Jose,

    Are you trying to redirect users to different pages after registration, depending on which registration role (which option in the dropdown they chose)?


    You could achieve that with a code snippet, as follows:

    add_filter( 'woocommerce_registration_redirect', 'custom_redirection_after_registration', 10, 1 );
    function custom_redirection_after_registration( $redirection_url ){
        // Change the redirection Url
        if (isset($_POST['b2bking_registration_roles_dropdown'])){
            if ($_POST['b2bking_registration_roles_dropdown'] === 'role_1234'){
                $redirection_url = '/page1';
            }
            if ($_POST['b2bking_registration_roles_dropdown'] === 'role_4567'){
                $redirection_url = '/page2';
            }
        }
        return $redirection_url; // Always return something
    }

    The above snippet redirects role_1234 to the site.com/page1 and redirect role_4567 to site.com/page2


    You can edit the above to match your specific roles and pages.


    For clarity, this is a PHP snippet, that can be added to functions.php, or by following our guide here: https://woocommerce-b2b-plugin.com/docs/how-to-add-a-snippet-php-or-js/



    Let me know if that can solve it for you,

    Kind regards,

    Stefan

  • Jose replied

    Thanks for the answer.

    To be more accurate, I want to redirect users after login, depending on which registration role they have.

    I have role "Club 1" with its specific url but I need to get role label from current user.

    I can use this function : 

    add_filter( 'woocommerce_login_redirect', 'custom_login_redirect', 10, 2 );
    function custom_login_redirect( $redirect, $user ) {
        if ($user->get_registration_role_label == 'Club 1')
        $redirect = "/page_club_1";
    }
    
  • Jose replied

    I've updated my last comment. I didn't notice there was a mistake...

  •  1,910
    WebWizards replied

    Thank you for clarifying,


    In that case, you can change the login redirect based on the registration role label as follows:

    add_filter( 'woocommerce_login_redirect', 'custom_login_redirect', 10, 2 );
    function custom_login_redirect( $redirect, $user ) {
        $registration_role = get_user_meta($user->ID, 'b2bking_registration_role', true);
        $registration_role_id = explode('_', $registration_role)[1];
        $registration_role_label = get_the_title($registration_role_id);
        if ($registration_role_label == 'Club 1'){
            $redirect = "/page_club_1";
        }
        return $redirect;
    }
    

    Please note that by "label", here I am referring to the title of the registration role (e.g. B2B (requires approval) below):

    4324525017.png


    Kind regards,

    Stefan