Redirect Users After WooCommerce Login or Registration

13 Shares

It’s possible to redirect your users to a page other than My Account when they login or register. Try adding the following code using the Code Snippets plugin, or via your preferred method.

Redirect at Login

To redirect at login, you need to use the woocommerce_login_redirect filter.

First of all, we’re checking whether the attempted redirect is for the checkout page; this catches users who are logging in at checkout, as it’s likely they will want to go back there and not some other page on the site. If they’re not at checkout, we redirect them to the WooCommerce shop page. You could even use the $user variable to redirect users based on their capabilities.

Redirect after Registration

To redirect after registration, you need to use the woocommerce_registration_redirect filter.

 

13 Shares

19 Comments

  1. Scott says:

    Hey, the redirect at login is exactly what I was looking for. Unfortunately this code breaks my site, and I cannot see where the problem lies.

  2. Brodie Brodie says:

    A semi colon was missing, this code works.
    /**
    * Redirect to shop after login.
    *
    * @param $redirect
    * @param $user
    *
    * @return false|string
    */
    function iconic_login_redirect( $redirect, $user ) {
    $redirect_page_id = url_to_postid( $redirect );
    $checkout_page_id = wc_get_page_id( ‘checkout’ );

    if( $redirect_page_id == $checkout_page_id ) {
    return $redirect;
    }

    return wc_get_page_permalink( ‘shop’ );
    }

    add_filter( ‘woocommerce_login_redirect’, ‘iconic_login_redirect’ );

  3. DOUDOU says:

    Hi ! Sorry I am a beginner at Woocommerce, website codes, etc. Could you tell me where I should write the code you gave ? Inside what folder, what file ?
    Thanks!

    • Edwin Bollen says:

      Either place this in the functions.php file or as James mentions use the Code Snippets plugin. (I prefer to use the last option)

    • James Kemp says:

      To confirm, that’s the functions.php file inside your theme (preferably child theme). Ideally any custom functionality would go in a plugin, but that’s not always straightforward for beginners.

  4. Bjornen says:

    Got a function which makes all accounts inactive after registration and after that, wp_logout() runs and after that, a redirect to the my account page. Since the user now is registered, logged out and re-directed back to the “My account” page where the login and register form is, do you know how to print a notice message on that page considering the session has ended?

    I’ve tried using this:
    if ( ! WC()->session->has_session() ) {
    WC()->session->set_customer_session_cookie(true);

    But that does not help. Any ideas or advice?

  5. Luc says:

    Hi, when I put this my checkout page gives a white page. Also what I really want is that when people come to the checkout page and they are not logged in the are redirected to the my account page to login first and when logged in they automatically come back to the checkout page.

    The first part I have, but how to redirect back to the checkout page is missing.
    Can you give me a hint?
    All the best, Luc

    //Woocommerce visible only to logged-in users
    function wpse_1_redirect() {
    if (
    ! is_user_logged_in()
    && (is_checkout())
    ) {
    // customize the following line to suit your needs
    wp_redirect(site_url(‘my-account/’));
    exit;
    }
    }
    add_action(‘template_redirect’, ‘wpse_1_redirect’);

  6. Ofer Ziv says:

    Nice one. thanks for that

  7. Scott says:

    When the Shop is assigned to Front page (home), it’s not redirecting to Home/Shop. Presumably because there’s no permalink for homepage. What might I do in this case?

  8. Yati says:

    Login redirection works well. I needed home_url() instead.
    Thank you

  9. Olivier says:

    Hi,

    Thank you very much for the code.
    I have a little issue though, I am using the registration redirection code but it’s taking me to the home page instead of the specified permalink.

    Below are the code I am using:

    function iconic_register_redirect( $redirect ) {
    return wc_get_page_permalink( ‘https://mysite.com/register/’ );
    }

    add_filter( ‘woocommerce_registration_redirect’, ‘iconic_register_redirect’ );

    I want this to redirect to the register page. I also tried to use ‘register’ but it’s still going to the home pate.

    Your assistance is highly appreciated.

    Thank you

    • James Kemp says:

      Hey,

      You’re using the wc_get_page_permalink() function incorrectly. This function only fetches WooCommerce pages and requires a Woo page slug.

      Instead you should try: return home_url( '/register/' );

      Hope this helps!

  10. NATO says:

    Hi .
    I am using the frontend pm plugin and woocommerce.

    If a customer goes to a product on my site there is a “contact seller” button which takes them to the frontend pm page to send a message to the seller. (if they are logged in)

    If a customer is not logged in the “contact seller” link will take them to a page saying ” you must login to use messages”.

    Once the customer then logs in they are redirected to there myaccount page.

    Is there a way to redirect after login to the frontend pm page they were originally aiming for ?

    thanks

  11. Cornel says:

    Make sure you replace:

    add_filter( 'woocommerce_login_redirect', 'iconic_login_redirect' );

    with:

    add_filter( 'woocommerce_login_redirect', 'iconic_login_redirect', 10, 2 );

    otherwise the $user param will not be available in the iconic_login_redirect function.

    More info here: https://developer.wordpress.org/reference/functions/add_filter/

Leave a Reply

Your email address will not be published. Required fields are marked *