Page 1 of 1

trigger event after customer login

Posted: Thu Nov 09, 2017 3:26 am
by tri1976
Opencart 3.0
I would like to run a function that uses the customer_id after the customer is logged in. Here is my setup

Code: Select all

event: 'my_event', 'catalog/controller/account/login/after', 'extension/module/my_event/updateCustomer'

function updateCustomer($route = '', $args = array(), $output = '') {
  $this->log-_write($route);
  $this->log-_write(print_r($args, true));
  $this->log-_write($output);
  $this->log-_write($this->customer->getId());
}
After the customer logged in, I see $route output "account/login" but nothing for $args, $output, or $this->customer->getId(). Am I doing something wrong? How do I get customer_id after the customer login event?

Thanks.

Re: trigger event after customer login

Posted: Thu Nov 09, 2017 4:17 am
by straightlight
What is the line of code you added in the account/login.php controller file?

Re: trigger event after customer login

Posted: Thu Nov 09, 2017 4:23 am
by tri1976
straightlight wrote:
Thu Nov 09, 2017 4:17 am
What is the line of code you added in the account/login.php controller file?
Nothing ... original files are not modified. I just want to run updateCustomer function after the customer is logged in. It looks like updateCustomer is run but I couldn't get customer_id using $this->customer->getId()

Re: trigger event after customer login

Posted: Thu Nov 09, 2017 4:33 am
by straightlight
function updateCustomer($route = '', $args = array(), $output = '') {
$this->log-_write($route);
$this->log-_write(print_r($args, true));
$this->log-_write($output);
$this->log-_write($this->customer->getId());
}
Should be changed for:

Code: Select all

function updateCustomer($route = '', $args = array(), $output = '') {
  $this->log->write($route);
  $this->log->write(print_r($args, true));
  $this->log->write($output);
  $this->log->write($this->customer->getId());
}
There are no underscores with methods in Opencart other than possible extensions. As for the $args not being passed, more information would need to be posted on your behalf since a single method does not demonstrate from which location you are actually launching the updateCustomer method.

Re: trigger event after customer login

Posted: Thu Nov 09, 2017 4:47 am
by tri1976

Code: Select all

$this->log-_write
was a typo. My actual code is

Code: Select all

$this->log->write
. The problem is the function is supposed to run after customer logged in but for some reason I can't get $this->customer->getId() to give me customer_id.

Re: trigger event after customer login

Posted: Thu Nov 09, 2017 4:49 am
by straightlight
The customer ID can be obtained once the page has been redirected and the customer logged in successfully. You can also verify with:

Code: Select all

if ($this->customer->isLogged()) {
    // Your argument here
}

Re: trigger event after customer login

Posted: Thu Nov 09, 2017 4:57 am
by tri1976
My function is supposed to be run after the login event but it doesn't look like it is. Using opencart event system, how do I get customer ID after the customer login?

Re: trigger event after customer login

Posted: Thu Nov 09, 2017 5:19 am
by straightlight
In an OCMod or VQMod file, you could always track this line from your system/library/cart/customer.php file,

find:

Code: Select all

private $address_id;
add below:

Code: Select all

private $registry;
Then, find:

Code: Select all

$this->session = $registry->get('session');
add below:

Code: Select all

$this->registry = $registry;
find in the login() method:

Code: Select all

$this->address_id = $customer_query->row['address_id'];
add below:

Code: Select all

$log = new Log('error.log');

$log->write($this->registry->get('request')->get['route']);
$log->write($this->customer_id);
This should already provide the results in the logs once the customer logs in.

Re: trigger event after customer login

Posted: Thu Nov 09, 2017 5:29 am
by tri1976
Well I didn't want to use vqmod or ocmod. Using the event system should have worked, shouldn't it? Why wouldn't it work? Am I missing something or is this a bug?

Re: trigger event after customer login

Posted: Thu Nov 09, 2017 8:13 am
by daveyoi
This after event will never fire when a customer logs in because the last action on the controller is a redirect back to account/account after a successful login - so it never returns to the router to trigger the after event.

Unfortunately, I don't think there is any event you can hook into which would only fire once, you could potentially listen on the account/account route but would then need to have some state logic in your observer to only action your code once per login. which makes things complex.

What actions are you looking to take in your updateCustomer observer?

Re: trigger event after customer login

Posted: Thu Nov 09, 2017 9:25 am
by tri1976
@straightlight and @daveyoi thanks for your reponses. I already found a workaround but it is just puzzling why it doesn't work. The "updateCustomer" function does get executed but not "after login" event. It ran sometime before the end of account/login function. I tried this event "catalog/controller/account/login/validate/after" but nothing happen.

Re: trigger event after customer login

Posted: Thu Nov 09, 2017 9:50 am
by straightlight
The provided solution above with either OCMod and VQMod should work just fine. It will add the events through the logs real-time when someone logs in.

Re: trigger event after customer login

Posted: Fri Nov 17, 2017 8:32 pm
by kathrynlee
Yes, straightlight, it works really fine. Useful solution

Re: trigger event after customer login

Posted: Sun Nov 21, 2021 11:08 am
by trueliar
tri1976 wrote:
Thu Nov 09, 2017 3:26 am
Opencart 3.0
I would like to run a function that uses the customer_id after the customer is logged in. Here is my setup

Code: Select all

event: 'my_event', 'catalog/controller/account/login/after', 'extension/module/my_event/updateCustomer'

function updateCustomer($route = '', $args = array(), $output = '') {
  
}
After the customer logged in, I see $route output "account/login" but nothing for $args, $output, or $this->customer->getId(). Am I doing something wrong? How do I get customer_id after the customer login event?

Thanks.


I've faced the same problem right now and I've found and using this function that is *always used after a login
catalog/model/account/customer/deleteLoginAttempts


// events after login
$this->model_setting_event->addEvent('my_event', 'catalog/model/account/customer/deleteLoginAttempts/after', 'extension/module/accountautodisabler/updateCustomer');


----
For events before the login there are different functions to check
catalog/controller/account/login/before
catalog/controller/checkout/login/save/before
catalog/controller/api/login/before
...
----

The events should work for OC 2.3.0.2 up to 3.0.3.8
I hope that those informations can be useful

Re: trigger event after customer login

Posted: Mon Nov 22, 2021 6:55 am
by straightlight
trueliar wrote:
Sun Nov 21, 2021 11:08 am
tri1976 wrote:
Thu Nov 09, 2017 3:26 am
Opencart 3.0
I would like to run a function that uses the customer_id after the customer is logged in. Here is my setup

Code: Select all

event: 'my_event', 'catalog/controller/account/login/after', 'extension/module/my_event/updateCustomer'

[b]function updateCustomer($route = '', $args = array(), $output = '') {
  
}[/b]
After the customer logged in, I see $route output "account/login" but nothing for $args, $output, or $this->customer->getId(). Am I doing something wrong? How do I get customer_id after the customer login event?

Thanks.


I've faced the same problem right now and I've found and using this function that is *always used after a login
catalog/model/account/customer/deleteLoginAttempts


// events after login
$this->model_setting_event->addEvent('my_event', 'catalog/model/account/customer/deleteLoginAttempts/after', 'extension/module/accountautodisabler/updateCustomer');


----
For events before the login there are different functions to check
catalog/controller/account/login/before
catalog/controller/checkout/login/save/before
catalog/controller/api/login/before
...
----

The events should work for OC 2.3.0.2 up to 3.0.3.8
I hope that those informations can be useful
The function part should be a method that states:

Code: Select all

public function updateCustomer(&$route, &$args, &$output) {
  
}
You are correct, however. These days, it is best to use Event Triggers with Opencart. The solution I provided above is quite old, which is why forum users should rather focus on up-to-date topics than falling back into the old ones most of the time.