Post by tri1976 » 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 = '') {
  $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.

email: trile7 at gmail dot com
Checkout My Extensions


User avatar
New member

Posts

Joined
Mon Mar 08, 2010 2:48 am

Post by straightlight » Thu Nov 09, 2017 4:17 am

What is the line of code you added in the account/login.php controller file?

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by tri1976 » Thu Nov 09, 2017 4:23 am

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

email: trile7 at gmail dot com
Checkout My Extensions


User avatar
New member

Posts

Joined
Mon Mar 08, 2010 2:48 am

Post by straightlight » Thu Nov 09, 2017 4:33 am

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.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by tri1976 » Thu Nov 09, 2017 4:47 am

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.

email: trile7 at gmail dot com
Checkout My Extensions


User avatar
New member

Posts

Joined
Mon Mar 08, 2010 2:48 am

Post by straightlight » Thu Nov 09, 2017 4:49 am

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
}

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by tri1976 » Thu Nov 09, 2017 4:57 am

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?

email: trile7 at gmail dot com
Checkout My Extensions


User avatar
New member

Posts

Joined
Mon Mar 08, 2010 2:48 am

Post by straightlight » Thu Nov 09, 2017 5:19 am

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.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by tri1976 » Thu Nov 09, 2017 5:29 am

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?

email: trile7 at gmail dot com
Checkout My Extensions


User avatar
New member

Posts

Joined
Mon Mar 08, 2010 2:48 am

Post by daveyoi » Thu Nov 09, 2017 8:13 am

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?

Image
Tristar Web Solutions


New member

Posts

Joined
Sun Oct 05, 2014 2:38 am

Post by tri1976 » Thu Nov 09, 2017 9:25 am

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

email: trile7 at gmail dot com
Checkout My Extensions


User avatar
New member

Posts

Joined
Mon Mar 08, 2010 2:48 am

Post by straightlight » Thu Nov 09, 2017 9:50 am

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.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by kathrynlee » Fri Nov 17, 2017 8:32 pm

Yes, straightlight, it works really fine. Useful solution

Hi! I have just created book of ra free play site so you can try to use my work


Newbie

Posts

Joined
Fri Nov 17, 2017 7:59 pm

Post by trueliar » 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'

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

New member

Posts

Joined
Sat Jan 11, 2014 12:09 am

Post by straightlight » Mon Nov 22, 2021 6:55 am

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.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON
Who is online

Users browsing this forum: nonnedelectari, Semrush [Bot] and 405 guests