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))

You like the tuts and want to say "thank you"? Well, you can always feel free to donate:

  • TelecUniversali

    Hi,
    It’s possible make a link to a extra tab of product?
    For example I have #idTab1 , #idTab2 and #ttdownload , but if i use the address of my productpage + #idTab2 dont go to open tab idTab2… what I can do?
    Thank you

  • Chris

    I’m trying to get this to work with a specific product but it’s not working out.

    if ($this->context->link->getProductLink(2) && preg_match(“/127.0.0.1/”, $_SERVER[‘REMOTE_ADDR’])) {
    Tools::redirect(‘http://prestashop.ipcentraal.nl/nl/home/1-faded-short-sleeves-tshirt.html’);
    } else {
    Tools::redirect(‘http://prestashop.ipcentraal.nl/nl/5-tshirts’);
    }

    I think this is supposed to work? but i keep getting redirected to a category instead of the product. The IP is just for showing, i wan’t to be able to redirect a certain IP range to a different product and if the ip doesnt fit the range it’s supposed to go to the normal product.

    Can you help me out?

  • http://www.fmemodules.com/ Amelia Johns

    I always visits the site to get help about PrestaShop, It’s new for me that redirection can be done locally in PrestaShop. This tutorial is quite helping for experts but for beginners, I think if you explain in more details [in steps], it will be more beneficial.

  • NemoPS

    Hey Pascal! Nice catch. I should have actually used the one I mentioned later

    $this->context->link->getProductLink(9)

    That said, it still worked for me as I had friendly urls, though it might create an un-necessary further redirect, so this one would be better

  • Pascal van Geest

    Hi Nemo,

    thanks for the post!

    One question. If you have turned on Friendly URL, can you still use:

    index.php?controller=product&id_product=1

    or should you use the friendly URL variant in that case?

    Just curious.

    Thanks for your Tutorials!
    pascal.

Store Top Sales

You like the tuts and want to say "thank you"? Well, you can always feel free to donate: