Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Redirect Users to Their Previous Page After WooCommerce Registration or Login?

One key aspect that a shop owner will be constantly trying is to improve the user experience of customers while they are shopping. Today we will explore such feature that can help you to achieve it. In this tutorial, we’ll show you how to automatically redirect customers to the page they were on before they logged in or registered using the default WooCommerce Login/Register forms.

Whether a guest user or a logged-out customer is selecting products or checking out promotions, they might want to log in or register to take advantage of an offer. With this functionality of redirecting to previous page , after logging in or registering, they won’t lose their place in the process. Instead, they will be redirected to the exact page they were on. Let’s see how this redirection works.

Solution: Redirect Users to Their Previous Page After WooCommerce Registration or Login

The code will help users redirect to the page before they were on after login or registration. So that once customers have completed the login or registration they will be redirected to the page they were viewing before and continue shopping without interruptions.

// Capture the referrer URL before login or registration
function ts_capture_referrer_url_for_auth() {
    if ( isset( $_SERVER['HTTP_REFERER'] ) && !empty( $_SERVER['HTTP_REFERER'] ) && !strstr( $_SERVER['HTTP_REFERER'], 'my-account' ) ) {
        set_transient( 'auth_referrer_url', esc_url( $_SERVER['HTTP_REFERER'] ), 60 * 60 * 24 ); // Store for 24 hours
    }
}
add_action( 'woocommerce_before_customer_login_form', 'ts_capture_referrer_url_for_auth' );
add_action( 'woocommerce_register_form', 'ts_capture_referrer_url_for_auth' );

// Redirect the user after login to the referrer URL
add_filter( 'woocommerce_login_redirect', 'ts_redirect_after_login_to_referrer', 10, 2 );
function ts_redirect_after_login_to_referrer( $redirect, $user ) {
    $referrer_url = get_transient( 'auth_referrer_url' );
    
    if ( $referrer_url ) {
        delete_transient( 'auth_referrer_url' ); // Clean up the transient after use
        return $referrer_url; // Redirect to the referrer URL
    }

    return $redirect; // Fallback to default WooCommerce redirect if no referrer
}

// Redirect the user after registration to the referrer URL
add_filter( 'woocommerce_registration_redirect', 'ts_redirect_after_registration_to_referrer' );
function ts_redirect_after_registration_to_referrer( $redirect ) {
    $referrer_url = get_transient( 'auth_referrer_url' );

    if ( $referrer_url ) {
        delete_transient( 'auth_referrer_url' ); // Clean up the transient after use
        return $referrer_url; // Redirect to the referrer URL
    }

    return $redirect; // Fallback to default WooCommerce redirect if no referrer
}

Output

Imagine a customer spots a great deal on your site and signs up to get it. Instead of ending up on the account page after signing up, the code takes them right back to the exact deal or product they were looking at. The same process repeats when a customer registers an account, they’re taken back to the page they were viewing before.

For stores aiming to direct new customers to a specific sales page or promotion after registration, implementing a custom redirection such as redirecting users to a specific page after registration in WooCommerce via code snippets can be highly effective. This approach allows you to guide users to targeted pages or special offers, potentially increasing engagement and conversions.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x