dtesmanovic Posted March 21, 2020 Share Posted March 21, 2020 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 More sharing options...
SmartPlugs Posted March 22, 2020 Share Posted March 22, 2020 Hi, I would achieve that with a module plugged on "actionDispatcher" hook (see Dispatcher::dispatch()). Otherwise with an override (FrontController::inti() as example). Yann Link to comment Share on other sites More sharing options...
dtesmanovic Posted March 23, 2020 Author Share Posted March 23, 2020 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 More sharing options...
SmartPlugs Posted March 23, 2020 Share Posted March 23, 2020 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 More sharing options...
dtesmanovic Posted March 25, 2020 Author Share Posted March 25, 2020 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 More sharing options...
SmartPlugs Posted March 26, 2020 Share Posted March 26, 2020 Hi, Wich version of PS do you use ? Is the rewrite mod enabled ? Yann Link to comment Share on other sites More sharing options...
dtesmanovic Posted March 27, 2020 Author Share Posted March 27, 2020 Hi Yann, The PS version is 1.7.5.1 and I use rewrite module "prettyurls". Thank you Link to comment Share on other sites More sharing options...
SmartPlugs Posted March 27, 2020 Share Posted March 27, 2020 Hi, As you use Pretty Urls i really would recommand to create a module hooks on actionDispatche Can you try a die('what you want') in the very begining of the init() override just to check if your class override works ? Yann Link to comment Share on other sites More sharing options...
dtesmanovic Posted March 27, 2020 Author Share Posted March 27, 2020 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 More sharing options...
SmartPlugs Posted March 28, 2020 Share Posted March 28, 2020 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 More sharing options...
dtesmanovic Posted July 4, 2020 Author Share Posted July 4, 2020 Hi @SmartPlugs, Sorry for opening this old post, but I still didn't get it. I put the registerHook and after that the hookFunction. I put there var_dump and die. No effect. Thank you Link to comment Share on other sites More sharing options...
dtesmanovic Posted December 2, 2020 Author Share Posted December 2, 2020 We can close this, I've made a solution by myself Thanks Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now