Woocommerce Hooks

Woocommerce hook to redirect user after login based on users

Woocommerce hook to redirect user after login based on users

woocommerce
260Views

Hello to all, welcome to therichpost.com. In this post, I will tell you, Woocommerce hook to redirect user after login based on users.

I am redirecting different users to different pages after woocommerce login based on usernames.
Here is the working code for Woocommerce hook to redirect user after login based on users and you need to add this code into your theme’s functions.php file:
function wc_custom_user_redirect( $redirect, $user ) {
    // Get user name
    $username = $user->user_login;

    // Conditions
    switch ( $username ) {
         case 'admin':
            $redirect = get_permalink( get_page_by_path( 'sample-page' ) );
            break;
         case 'username1':
            $redirect = get_permalink( get_page_by_path( 'sample-page-2' ) );
            break;
         case 'username2':
            $redirect = get_permalink( get_page_by_path( 'sample-page-3' ) );
            break;
         case 'username3':
            $redirect = get_permalink( get_page_by_path( 'sample-page-4' ) );
            break;
         default:
            $redirect = wp_get_referer() ? wp_get_referer() : home_url();
    }
    return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'wc_custom_user_redirect', 10, 2 );

 If you have any query related to this post, then please comment or email me.

 

 

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am full stack developer. I love coding. I know Wordpress, Core php, Angularjs, Angular 2, Angular 6, Angular 7, laravel, Codeigniter, Shopify, Squarespace, Jquery, Google Map Api, Vuejs, Reactjs, Bigcommerce etc.

Leave a Reply

Positive SSL