Opencart Login to Previous Viewed Page After Logging in?
Anyone know a way this can be done? Working on Version 1.5.4
Hope someone can advise/help!
Thanks!
Opencart Login to Previous Viewed Page After Logging in?
Anyone know a way this can be done? Working on Version 1.5.4
Hope someone can advise/help!
Thanks!
To achieve this scenario:
You would have to edit catalog/controller/account/login.php controller. Add this right after the public function index() { line:
if(!isset($this->session->data['login_redirect'])
&& (strpos($_SERVER['HTTP_REFERER'], HTTP_SERVER) !== false
|| strpos($_SERVER['HTTP_REFERER'], HTTPS_SERVER) !== false)) {
$this->session->data['login_redirect'] = $_SERVER['HTTP_REFERER'];
}
Please be wise of $_SERVER['HTTP_REFERER'] may not contain any URL depending on the HTTP server and/or browser used thus this may not work all the time (and there is no other chance to retrieve the URL user came from).
Now in that same file find the line:
$this->redirect($this->url->link('account/account', '', 'SSL'));
(should be line 59, my version is 1.5.5.1) and change it to:
if(!empty($this->session->data['login_redirect'])) {
$url = $this->session->data['login_redirect'];
unset($this->session->data['login_redirect']);
$this->redirect($url);
} else {
$this->redirect($this->url->link('account/account', '', 'SSL'));
}
This should work in most cases.
Thanks to shadyyx's answer above. I had too little rep power to up-vote it, so instead I'm posting my vQmod code here as a thank you.
Note that the code below replaces all 3 instances of $this->redirect($this->url->link('account/account', '', 'SSL')) since replacing only the first instance did not work for me (it has not been exhaustively tested).
<?xml version="1.0" encoding="UTF-8"?>
<modification>
<id>Login Redirects (to where user was prior to login)</id>
<version>1</version>
<vqmver>2.4.1</vqmver>
<author>Salad_Dressing (with help from shadyyx)</author>
<file name="catalog/controller/account/login.php">
<operation>
<search position="after"><![CDATA[
public function index() {
]]></search>
<add><![CDATA[
if(!isset($this->session->data['login_redirect']) && (strpos($_SERVER['HTTP_REFERER'], HTTP_SERVER) !== false || strpos($_SERVER['HTTP_REFERER'], HTTPS_SERVER) !== false)) {
$this->session->data['login_redirect'] = $_SERVER['HTTP_REFERER'];
}
]]></add>
</operation>
<operation>
<search position="replace"><![CDATA[
$this->redirect($this->url->link('account/account', '', 'SSL'));
]]></search>
<add><![CDATA[
if(!empty($this->session->data['login_redirect'])) {
$url = $this->session->data['login_redirect'];
unset($this->session->data['login_redirect']);
$this->redirect($url);
} else {
$this->redirect($this->url->link('account/account', '', 'SSL'));
}
]]></add>
</operation>
</file>
</modification>