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

14 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

Leave a Reply

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