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?

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Opencart.com Administrator / Quality Assurance Analyst / Programmer


Administrator

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.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Opencart.com Administrator / Quality Assurance Analyst / Programmer


Administrator

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
}

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Opencart.com Administrator / Quality Assurance Analyst / Programmer


Administrator

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.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Opencart.com Administrator / Quality Assurance Analyst / Programmer


Administrator

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.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Opencart.com Administrator / Quality Assurance Analyst / Programmer


Administrator

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
Who is online

Users browsing this forum: thekrotek and 30 guests