Jump to content

Private shop redirect to login and come back to previous URL


arkantos_a

Recommended Posts

When I enter an existing URL of a category without login in my private prestashop website, fronController redirects to login page (THIS IS OK):

if (!$this->context->customer->isLogged() && $this->php_self != 'authentication' && $this->php_self != 'password')
        {
            Tools::redirect('index.php?controller=authentication?back=my-account');

 

But after login I would like to come back to the first URL of my category. Can you help with this? Thanks in advance

 

PD: I have tried this without succes Tools::redirect('index.php?controller=authentication?back='.$_SERVER["REQUEST_URI"]);

Edited by arkantos_a
Update (see edit history)
Link to comment
Share on other sites

  • 1 month later...

Before redirecting the user to the login page, store the current category URL in a session variable:

if (!$this->context->customer->isLogged() && $this->php_self != 'authentication' && $this->php_self != 'password')
{
    // Store the current category URL in a session variable
    $category_url = $_SERVER["REQUEST_URI"];
    $this->context->cookie->category_url = $category_url;
    Tools::redirect('index.php?controller=authentication?back=my-account');
}

After a successful login, retrieve the stored category URL from the session and redirect the user back to it:

// handle successful login
// ...

// Check if the session variable with the category URL exists
if (isset($this->context->cookie->category_url) && !empty($this->context->cookie->category_url))
{
    // Retrieve the category URL from the session
    $category_url = $this->context->cookie->category_url;

    // Clear the session variable to avoid redirecting the user to the category on future logins
    $this->context->cookie->category_url = '';

    // Redirect the user back to the stored category URL
    Tools::redirect($category_url);
}
else
{
    // If no stored category URL found, redirect the user to the default page after login (e.g., My Account)
    Tools::redirect('index.php?controller=my-account');
}

I hope this helps.

Thanks!

  • Thanks 1
Link to comment
Share on other sites

3 hours ago, AddWeb Solution said:

Before redirecting the user to the login page, store the current category URL in a session variable:

if (!$this->context->customer->isLogged() && $this->php_self != 'authentication' && $this->php_self != 'password')
{
    // Store the current category URL in a session variable
    $category_url = $_SERVER["REQUEST_URI"];
    $this->context->cookie->category_url = $category_url;
    Tools::redirect('index.php?controller=authentication?back=my-account');
}

After a successful login, retrieve the stored category URL from the session and redirect the user back to it:

// handle successful login
// ...

// Check if the session variable with the category URL exists
if (isset($this->context->cookie->category_url) && !empty($this->context->cookie->category_url))
{
    // Retrieve the category URL from the session
    $category_url = $this->context->cookie->category_url;

    // Clear the session variable to avoid redirecting the user to the category on future logins
    $this->context->cookie->category_url = '';

    // Redirect the user back to the stored category URL
    Tools::redirect($category_url);
}
else
{
    // If no stored category URL found, redirect the user to the default page after login (e.g., My Account)
    Tools::redirect('index.php?controller=my-account');
}

I hope this helps.

Thanks!

Thanks you very much, following those steps works perfectly!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...