
Prestashop Redirections
In this flash tip we will see how to deal with Prestashop redirections in the cleanest possible way, and send customers where we want after login as well.
Redirecting in Prestashop
Using normal redirections with the php header method in Prestashop is highly not recommended. Instead, Prestashop has its own methods to redirect users to the page you want, specifically for the front and back office.
Redirecting users in the Front office after login
We can use two methods to manage Prestashop Redirections in the front office:
- Tools::redirect($url)
- Tools::redirectLink($url)
The first one is what I generally use:
Tools::redirect('order');
redirectLinkcan be used the same way. However, the previous one also allows us to use a custom base url, a link class and custom headers:
Tools::redirect($url, $base_uri = __PS_BASE_URI__, Link $link = null, $headers = null)
They are generally not needed, but might come in handy for specific cases. If you only use the page name, like I used order above, make sure it exists within the shop. Therefore, something like the following will not work (unless you really setup a specific htaccess rule for it)
Tools::redirect('ordersomethingelse');
And you will end up with a 404 page
Knowing that, let’s see how to redirect our users to a custom page after logging in. We will not use overrides to be quicker.
Open up AuthController.php located in controllers/front. Locate the following, around line 620:
if (($back = Tools::getValue('back')) && $back == Tools::secureReferrer($back)) Tools::redirect(html_entity_decode($back)); // redirection: if cart is not empty : redirection to the cart if (!$this->ajax && $back = Tools::getValue('back')) { if ($back == Tools::secureReferrer(Tools::getValue('back'))) Tools::redirect(html_entity_decode($back)); $back = $back ? $back : 'my-account'; Tools::redirect('index.php?controller='.(($this->authRedirection !== false) ? urlencode($this->authRedirection) : $back)); }
We can get rid of most of it as we really want users to reach our custom page (in this case, a product)
if (!$this->ajax) { Tools::redirect('index.php?controller=product&id_product=1'); }
This, will, no matter what, send users to that product’s page when they login
Prestashop redirection in the Back Office
To redirect to a page of the admin interface, it will be enough to use the following
Tools::redirectAdmin($url);
However, retrieving the url might be a bit troublesome since Prestashop uses a token to secure back office pages. In this case, we first need to grab the url as follows:
$link = $this->context->link->getAdminLink('AdminProducts'); Tools::redirectAdmin($link);
$link will contain the needed token so that your back office page won’t crash to an “invalid token” error.
Always retrieving correct links
Here is a list of methods you can use to retrieve various entities’ links in Prestashop (please make sure you use the correct context):
// page link, like 'index', 'order'... $this->context->link->getPagelink('pagename') // CMS page link, first parameter being the cms id $this->context->link->getPagelink(9) // Various entities, with id as the parameter $this->context->link->getSupplierLink(9) $this->context->link->getManufacturerLink(9) $this->context->link->getProductLink(9) $this->context->link->getCategoryLink(9) // Modules, with parameters like ('modulename', 'controller', array(1,2,3)), the array being a list of querystring parameters $this->context->link->getModuleLink('psticket', 'mytickets', array('id_ticket' => 1, 'example' => 1))