Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls

Redirecting a customer to a custom page after login, depending on parameter
 
Reply
   X-Cart forums > X-Cart 5 > Modifying the design and features (X-Cart 5)
 
Thread Tools Search this Thread
  #1  
Old 11-07-2019, 12:04 PM
 
Ed B. Ed B. is offline
 

eXpert
  
Join Date: Apr 2016
Posts: 229
 

Default Redirecting a customer to a custom page after login, depending on parameter

I would like to redirect customers after login to custom url according to a parameter.


Now, I can do custom redirection, as follows.


In /XLite/Module/VendorID/ModuleID/Controller/Customer/Login.php


Code:
<?php namespace XLite\Module\VendorID\ModuleID\Controller\Customer; class Login extends \XLite\Controller\Customer\Login implements \XLite\Base\IDecorator { protected function loginBody() { { $this->profile = $this->performLogin(); $profileCart = $this->getCart(); $this->setReturnURL( \XLite\Core\Converter::buildFullURL('mypage') ); $this->setHardRedirect(); } } }
(This time, I didn't forget "implements \XLite\Base\IDecorator" part).


However, the following code doesn't work.
Code:
<?php namespace XLite\Module\VendorID\ModuleID\Controller\Customer; class Login extends \XLite\Controller\Customer\Login implements \XLite\Base\IDecorator { protected function loginBody() { if(\XLite\Core\Request::getInstance()->param == "myparam") { $this->profile = $this->performLogin(); $profileCart = $this->getCart(); $this->setReturnURL( \XLite\Core\Converter::buildFullURL('mypage') ); $this->setHardRedirect(); } else { parent::loginBody() } } }
and the customer logs in to cart.php?target=login&param=myparam,
the customer doesn't get redirected to cart.php?target=mypage, instead
the customer gets redirected to cart.php (?target=main is implicit).


What am I doing here this time?


Of course, I could write another class, say mylogin.php so that the customer can
log in to cart.php?target=mylogin to get redirected to cart.php?target=mypage,
but this is not elegant or economic. Furthermore, in the module I am currently
working with, there are only two pages to redirect, but in another module in my project, there are more pages and I would rather not write one class for each page

to redirect... Would anyone have an idea?
__________________
X-cart 5.2.12, php 5.6
Ed from Grenoble, France
Reply With Quote
  #2  
Old 11-07-2019, 12:12 PM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 13,616
 

Default Re: Redirecting a customer to a custom page after login, depending on parameter

First make sure this function is fit when someone logs in.
Then dump the Request so you can see what's being passed on.
Then make sure that - $this->performLogin() - is not doing the redirect - in that case your code below will not run.
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote
  #3  
Old 11-08-2019, 12:27 AM
 
Ed B. Ed B. is offline
 

eXpert
  
Join Date: Apr 2016
Posts: 229
 

Default Re: Redirecting a customer to a custom page after login, depending on parameter

Quote:
Originally Posted by cflsystems
First make sure this function is fit when someone logs in.
Then dump the Request so you can see what's being passed on.
Then make sure that - $this->performLogin() - is not doing the redirect - in that case your code below will not run.
  • The function is hit when someone logs in, this is certain. Otherwise the my first code (unconditional redirection) wouldn't work
  • $this->performLogin() - is not doing the redirect, for the same reason.
  • Somehow I have been unable to dump what is in \XLite\Core\Request::getInstance() (I elaborate on this later), but then, it seems the parameter isn't passed to the controller at all. That is. if I write
    Code:
    if (isset(_GET['param'])
    it still doesn't work.
So, my parameter isn't passed to the controller. But in the URL, it is there (cart.php?target=login&param=myparam). What can I do with this?


As to my impossible debugging, putting the dump function within the controller,
anywhere during the login process simply makes logging impossible. I also tried to
write a viewer class XLite/Module/VendorID/ModuleID/View/Login.php
Code:
<?php namespace XLite\Module\VendorID\ModuleID\View; /** * @ListChild (list="center", zone="customer", weight="101") */ class Login extends \XLite\View\Login { public static function getAllowedTargets() { $instance = \XLite\Core\Request::getInstance(); $dump = \Doctrine\Common\Util\Debug::dump($instance); echo $dump; return parent::getAllowedTargets(); } public function getParamValue() { return \XLite\Core\Request::getInstance()->param; } protected function getDefaultTemplate() { return 'modules/VendorID/dump.twig'; } }
(this time, the class doesn't have to decorate XLite/View/Login, but I tried both just
in case) echo $dump isn't really in a good place, but the function getAllowedTargets
gets always hit whenever the class is called, so I should get the result of dump
if not visible on the page, at least in the html source code somewhere. The twig file
isn't called, and probably viewer class. So what am I doing wrong again?
__________________
X-cart 5.2.12, php 5.6
Ed from Grenoble, France
Reply With Quote
  #4  
Old 11-08-2019, 05:12 AM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 13,616
 

Default Re: Redirecting a customer to a custom page after login, depending on parameter

Forget about \Doctrine\Common\Util\Debug::dump - it works but it is not pretty and messes up with the code. And you can't use "echo" unless you use "exit" after it as the execution continues after the echo and it is lost.


Install Symfony Dumper component - https://symfony.com/doc/current/components/var_dumper.html
- I would install it globally for your working environment that way you can use it every time for every project.

Then you can use



Code:
dump(Request);


to dump the request without interrupting the execution. You can also use



Code:
dd(Request);


which will dump the variable and exit. You can also use it within assigns as the dump function returns whatever you are dumping


Code:
$request = dump(Request);


$request will be assigned the value of Request and value will be dumped, that way you get to see what Request holds without stopping the normal execution of the process.


You can also use it in twig files...
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote

The following user thanks cflsystems for this useful post:
Ed B. (11-10-2019)
  #5  
Old 11-10-2019, 08:54 AM
 
Ed B. Ed B. is offline
 

eXpert
  
Join Date: Apr 2016
Posts: 229
 

Default Re: Redirecting a customer to a custom page after login, depending on parameter

Thank you very much, this tool is really awesome! So here is what seems to be happening.


The function loginBody() (or the doActionLogin() which calls this function) is called when a customer goes to page cart.php?target=login&param=myparam, and click the "Sign in" button. So the instance moves to cart.php?target=login&action=login, and "param" is lost.


There is "fromURL" which is in the instance that I could use if I don't find anything better, but I would like to use directly the value of "param". Obvious thing to do is to get it from the viewer class, but somehow I can't call a viewer class for the target "login". Decorating the class /View/Login.php only modifies admin pages, even if I change the zone in ListChild declaration. And, in any case I will want to change the content of the page cart.php?target=login sooner or later. Any advice on how to call the viewer class for this page?
__________________
X-cart 5.2.12, php 5.6
Ed from Grenoble, France
Reply With Quote
  #6  
Old 11-10-2019, 09:15 AM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 13,616
 

Default Re: Redirecting a customer to a custom page after login, depending on parameter

You will have to declare your param in order to use it. Look at XLite\Controller\Customer\Login how params are listed then follow back the class path how they are declared. And used.
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote
  #7  
Old 11-11-2019, 07:40 AM
 
Ed B. Ed B. is offline
 

eXpert
  
Join Date: Apr 2016
Posts: 229
 

Default Re: Redirecting a customer to a custom page after login, depending on parameter

How the params are treated look extremely confusing to me (somehow the values of the array end up becoming keys going through some functions), but, does this really solve my problem? I can declare param like
Code:
protected $params = array('target','mode','param');
then I can only change this array within some functions, otherwise I get a syntax error ( unexpected '$params' (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST))


and if I try to set the array directly like

Code:
protected $params = $this->getAllParams();
then I get
PHP Fatal error: Constant expression contains invalid operations

And, of course, if I play with $params etc. within my function loginBody(),
then I will have the same problem as before...
__________________
X-cart 5.2.12, php 5.6
Ed from Grenoble, France
Reply With Quote
  #8  
Old 11-11-2019, 08:12 AM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 13,616
 

Default Re: Redirecting a customer to a custom page after login, depending on parameter

Try not to use 'param' as keyword as this may be "reserved" word in XC
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote
  #9  
Old 11-11-2019, 10:43 AM
 
Ed B. Ed B. is offline
 

eXpert
  
Join Date: Apr 2016
Posts: 229
 

Default Re: Redirecting a customer to a custom page after login, depending on parameter

Changing the parameter name didn't help. I managed to place $this->getAllParams
in the main body of the class (using __construct), but it still picks up the parameters for login action, not the ones for arriving the login form page.



I ended up with the following code.
Code:
public function getParam() { $instance = \XLite\Core\Request::getInstance(); $fro = $instance->getData(); $url = $fro["returnURL"]; $from = parse_url($url, PHP_URL_QUERY); $pieces = explode("&",$from); foreach($pieces as $key => $value) { $part=explode("=",$value); if ($part[0]=="param") {return $part[1];} } return null; }
This does the job, but I feel somehow stupid... Anyway, thank you very much for all your help and your patience.
__________________
X-cart 5.2.12, php 5.6
Ed from Grenoble, France
Reply With Quote
  #10  
Old 11-11-2019, 04:20 PM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 13,616
 

Default Re: Redirecting a customer to a custom page after login, depending on parameter

If it is not working you are doing something wrong or in the wrong place. You need to follow the stock code and find where customer gets redirected. At login if customer already has cart the redirection is to the cart page otherwise to the page from which login was called. If it is new customer and this is "register" the redirection is to the account page.

Check the handleRequest() method as this is going to be where the request is being redirected to one place or another then just follow the code. Your parameter is not stripped form the url it is just that the redirects are hardcoded to specific page and existing url params are not passed on to the redirected url.
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote
Reply
   X-Cart forums > X-Cart 5 > Modifying the design and features (X-Cart 5)


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 07:20 PM.

   

 
X-Cart forums © 2001-2018