Categories

Sunday, February 5, 2023
+919814419350 therichposts@gmail.com
Woocommerce Hooks

Woocommerce hook to redirect user after login based on users

How to add download pdf link in woocommerce single product page?

Woocommerce hook to redirect user after login based on users

2.4K

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.

 

 

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 8, Angular 9, Angular 10, Angular 11, Angular12, Angular 13, Angular 14, Angular 15, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.