Jump to content

Voucher link triggered before NotFoundPage


dtesmanovic

Recommended Posts

Hello all,

I have now coupons activated by link if the url have param "voucher" in the url. What I wanted to achieve right now is that if my page for an example: www.example.com/en/tesmanovic, where "tesmanovic" is coupon code. I want from Prestashop first to check if the thing after / is inside the coupon, activate the coupon and redirect it to the homepage instead of showing the 404 Not found page, because /tesmanovic doesn't exist as a page. So I want to trigger voucher/coupon code check before it retrieve the 404 not found. I guess this is the controller change. Could anyone point me some directions?

 

Thank you

Link to comment
Share on other sites

21 hours ago, SmartPlugs said:

Hi,

I would achieve that with a module plugged on "actionDispatcher" hook (see Dispatcher::dispatch()).
Otherwise with an override (FrontController::inti() as example).

Yann

 

Hello, thanks for your answer!

How you think about an override? I don't see in the init() function redirect for 404 not found pages, how would you create a workaround for that? Check the url, see if the code exist in the coupons and then make a redirection to the home? I mean that would be a multiple redirection, am I right?

Thank you

Link to comment
Share on other sites

Hi,

The PageNotFoundController class extends FrontController class.
So the init() method in PageNotFoundController is herited from the FrontController one and i would override this one.

The override method would be something like 

public function init()
{
parent::init();
Your code here 
Then Redirect to index 
}

The good point with the Dispatcher way is that you wouldn't have to make a redirect (just change the controller).
The good one with the FrontController override is that all objects like cart, customer, cookie etc will be already instanciated.

There're probably many ways to achieve your project, i just gave the "first" ideas i would have.

Yann

 

 

 

Link to comment
Share on other sites

On 3/23/2020 at 8:33 PM, SmartPlugs said:

Hi,

The PageNotFoundController class extends FrontController class.
So the init() method in PageNotFoundController is herited from the FrontController one and i would override this one.

The override method would be something like 


public function init()
{
parent::init();
Your code here 
Then Redirect to index 
}

The good point with the Dispatcher way is that you wouldn't have to make a redirect (just change the controller).
The good one with the FrontController override is that all objects like cart, customer, cookie etc will be already instanciated.

There're probably many ways to achieve your project, i just gave the "first" ideas i would have.

Yann

 

 

 

Hi Yann,

 

What I was talking about is for an example if I put this:

public function init()
    {
        parent::init();

        $request = parse_url($_SERVER['REQUEST_URI']);
        $path = $request["path"];
        $result = rtrim(str_replace(basename($_SERVER['SCRIPT_NAME']), '', $path), '/');
        var_dump($result);
        header("Location: http://www.mysite.com/");
	}

and then I go to www.mysite.com/tesmanovic ,where tesmanovic should be in $result from var_dump, but instead it retrieves "index.php?controller=404" in var_dump, since the /tesmanovic doesn't exists. Even if I put die(); after var_dump, it will still get the "index.php?controller=404". Do you have some thoughts on this?

 

Thank you

Link to comment
Share on other sites

Hi Yann,

Thank you for your fast responses. If I put first thing in init() of the FrontController override the var_dump() and then die(). It print some text and then don't load the website. But if I type for an example www.myserver.com/test and the test is not exist, the url will be www.myserver.com/index.php?controller=404 and the page will not be loaded, but again it first change the url and then die :/ I didn't do any actionDispatcher, could you point me some example or something if you think that's better solution?


Thank you

Link to comment
Share on other sites

 

Hi,

I just read in PS 1.7.4.2 code that there's a redirect to index.php?controller=404 on ProductController when the product does not exist. This is quite different of what is done in PS 1.6 that just returns & 404 code and then display an error whitin the product page (controller) and i did not noticed that before.
Anyway,  i was expecting that putting a die() in the begining of FrontController::init() override should have avoid to be redirected to the 404 page.
If i were you i would do some step by step debug to understand what's going on (and if you want to use an override).

Using a module hooked on actionDispatcher will allow you to play before the init() method. Pretty urls already override many controllers so ...
It's a bit more longer in terms of code but the idea is to build a module (http://doc.prestashop.com/pages/viewpage.action?pageId=51184607). This module should declare that it is hooked on actionDispatcher on his install method
 

!$this->registerHook('actionDispatcher')

And then it should have a hookActionDispatcher() method

public function hookActionDispatcher($args)
{
	do what you need to do like a redirect or running your own controller
}

Yann
 

 

 

 

 

Link to comment
Share on other sites

  • 3 months later...
  • 4 months later...

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