2

I have a woocommerce shop that only allows logged in users to make purchases. I have replaced my add to cart button with a "login" button if the user is not already logged in. This than brings them to the (my-account) page where they can choose to login or register.

However I was wondering if I could take this one step further and make it so wordpress remembers what page the user was on before they entered the login page. And if they login, it will redirect them back to that page.

I found this script which redirects users to the login page if they click on a product/page. And then if they login it takes them back to the page they were trying to enter. This is close to what I want to do, but instead of taking them to the login page before seeing a product. I want them to be able to see the product. Then click a button that takes them to the ((my-account page)) and if they login it redirects them back to the product page.

Redirect user to original url after login?

Any help would be much appreciate. Function, plugins, anything.

Thanks!

  • How is your login form implemented? I don't see any code in your question :( – Tom J Nowell Jul 21 '18 at 2:18
  • Its just the native "my-account" login form page that comes with woocommerce – Patrick Jul 21 '18 at 2:19
  • All woocommerce login pages are the same accross most themes. Unless the theme changes it. Mine just uses the native woocommerce login form. Like: themes.woocommerce.com/storefront/my-account – Patrick Jul 21 '18 at 2:22
2

You can use wordpress referrer function. For example set referrer URL in session by clicking on add to cart button or any button to store previous URL. Once login or register success add Woocommerce success login redirect to that URL.

For more information read WordPress referrer URL read here;

https://codex.wordpress.org/Function_Reference/wp_get_referer

For Woocommerce login redirect read here;

https://stackoverflow.com/questions/29342186/woocommerce-after-login-redirect

2

For anyone else looking for a solution to this. My boyfriend and I have solved it with the below script. Simply paste into your functions.php file for any woocommerce setup.

/** Log in redirect to previous page by portalpacific.net **/
// start global session for saving the referer url
function start_session() {
    if(!session_id()) {
        session_start();
    }
}
add_action('init', 'start_session', 1);

// get the referer url and save it to the session
function redirect_url() {
    if (! is_user_logged_in()) {
        $_SESSION['referer_url'] = wp_get_referer();
    } else {
        session_destroy();
    }
}
add_action( 'template_redirect', 'redirect_url' );

//login redirect to referer url
function login_redirect() {
    if (isset($_SESSION['referer_url'])) {
        wp_redirect($_SESSION['referer_url']);
    } else {
        wp_redirect(home_url());
    }
}
add_filter('woocommerce_login_redirect', 'login_redirect', 1100, 2);

/** end here */

Thanks for getting us started Karen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.