Unable to trigger event(post.customer.login and post.customer.logout) in Opencart 3.0.2.0

996 Views Asked by At

I want to set the session after user login in opencart-3.0.2.0

I am new to opencart, I have just created this two files only in the corresponding folder.anything else I need to be done to trigger the event.

I am referring this link to trigger the event in opencart:https://isenselabs.com/posts/opencart2-event-system-tutorial

I have searched a lot on google still no result found.

Code that I am using to trigger event in opencart.

path : admin/controller/module/mymodule.php

Code :

    public function install() {
        $this->load->model('extension/event');
        $this->model_extension_event->addEvent('mymodule', 'pre.admin.store.delete', 'module/mymodule/on_store_delete');
        $this->model_extension_event->addEvent('mymodule', 'post.customer.login', 'module/mymodule/post_customer_login_customtoken');
        $this->model_extension_event->addEvent('mymodule', 'post.customer.logout', 'module/mymodule/post_customer_logout_function');
    }

    public function uninstall() {
        $this->load->model('extension/event');
        $this->model_extension_event->deleteEvent('mymodule');
    }

    public function on_store_delete($store_id) {
        $this->load->model('setting/store');
        $store_info = $this->model_setting_store->getStore($store_id);
        $admin_mail = $this->config->get('config_email');
        mail($admin_mail, "A store has been deleted", "The store " . $store_info['url'] . " was deleted.");
    }
}

path : catalog/controller/module/mymodule.php

Code :

<?php
class ControllerModuleMyModule extends Controller {
    public function post_customer_login_customtoken() {
        $str = 'abcdefghigklmnopqrstuvwxyz';
        $shuffled = str_shuffle($str);
        $this->session->data['custom_token'] = $shuffled;
    }

    public function post_customer_logout_function(){
        $this->log->write("post_customer_logout_function");
        unset($this->session->data['custom_token']);
    }
}
1

There are 1 best solutions below

4
On BEST ANSWER

That tutorial is for OpenCart 2.0 - 2.1, in OpenCart 2.2 and above Event system has been changed.

For OpenCart 3.0.2.0 instead of:

$this->load->model('extension/event');
// and
$this->model_extension_event->addEvent

use:

$this->load->model('setting/event');
// and
$this->model_setting_event->addEvent

Instead of:

'post.customer.login'

use:

'catalog/controller/account/login/after'

Instead of:

deleteEvent

Use:

deleteEventByCode

So it must be:

admin\controller\extension\module\mymodule.php

public function install(){
    $this->load->model('setting/event');
    $this->model_setting_event->addEvent('mymodule', 'catalog/controller/account/login/after', 'extension/module/mymodule/after_customer_login_customtoken');
}
public function uninstall(){
    $this->load->model('setting/event');
    $this->model_setting_event->deleteEventByCode('mymodule');
}


catalog\controller\extension\module\mymodule.php

class ControllerExtensionModuleMyModule extends Controller {
    public function after_customer_login_customtoken(){
        $this->log->write('test');
    }
}