Search code examples
opencartopencart-3opencart-module

opencart customer Events, not being called in admin side


I using OC3, and trying to handle addCustomer event to move the newly created customers to the ERP system. I have created an extension admin/controller/extension/module/erp_integration.php this extension has the registration functionality for the events and the calls for products integration.

class ControllerExtensionModuleErpIntegration extends Controller {
    private $error = array();    


    public function index() {}

    public function validate() {}

    public function install() {
        $this->load->model('setting/event');
        $this->model_setting_event->addEvent('product_notification', 'admin/model/catalog/product/addProduct/after', 'extension/module/erp_integration/addProduct');
        $this->model_setting_event->addEvent('product_notification', 'admin/model/catalog/product/editProduct/after', 'extension/module/erp_integration/editProduct');
        $this->model_setting_event->addEvent('customer_notification', 'catalog/model/account/customer/addCustomer/after', 'extension/module/erp_integration/addCustomer');
        $this->model_setting_event->addEvent('customer_notification', 'catalog/model/account/customer/editCustomer/after', 'extension/module/erp_integration/editCustomer');

        $this->model_setting_event->addEvent('order_notification', 'catalog/model/checkout/order/addOrder/after', 'extension/module/erp_integration/addOrder');
    }

    public function uninstall() {
        $this->load->model('setting/event');
        $this->model_setting_event->deleteEventByCode('product_notification');
        $this->model_setting_event->deleteEventByCode('customer_notification');
        $this->model_setting_event->deleteEventByCode('order_notification');

    }

    // admin/model/catalog/product/addProduct/after
    public function addProduct(&$route, &$args, &$output)  {
        file_put_contents ( "testing.txt","\n******ADD PRODUCT**********\n" ,FILE_APPEND );
        file_put_contents ( "testing.txt",print_r($args,true) . print_r($output,true) ,FILE_APPEND );
    }

    // admin/model/catalog/product/editProduct/after
    public function editProduct(&$route, &$args, &$output)  {
        file_put_contents ( "testing.txt","\n******EDIT PRODUCT**********\n" ,FILE_APPEND );
        file_put_contents ( "testing.txt",print_r($args,true) . print_r($output,true) ,FILE_APPEND );
    }

    // catalog/model/account/customer/addCustomer/after
    public function addCustomer(&$route, &$args, &$output)  {
        file_put_contents ( "testing.txt","\n******ADD CUSTOMER from admin**********\n" ,FILE_APPEND );
        file_put_contents ( "testing.txt",print_r($args,true) . print_r($output,true) ,FILE_APPEND );
    }

}

For the customer part I created another file in catalog/controller/extension/module/erp_integration.php to handle the customer events

<?php

class ControllerExtensionModuleErpIntegration extends Controller {

    // catalog/model/account/customer/addCustomer/after
    public function addCustomer(&$route, &$args, &$output)  {
        file_put_contents ( "testing.txt","\n******ADD CUSTOMER**********\n" ,FILE_APPEND );
        file_put_contents ( "testing.txt",print_r($args,true) . print_r($output,true) ,FILE_APPEND );
    }

    // catalog/model/account/customer/editCustomer/after
    public function editCustomer(&$route, &$args, &$output)  {
        file_put_contents ( "testing.txt","\n******EDIT CUSTOMER**********\n" ,FILE_APPEND );
        file_put_contents ( "testing.txt",print_r($args,true) . print_r($output,true) ,FILE_APPEND );

    }

    // catalog/model/checkout/order/addOrder/after
    public function addOrder(&$route, &$args, &$output)  {
        file_put_contents ( "testing.txt","\n******ADD ORDER**********\n" ,FILE_APPEND );
        file_put_contents ( "testing.txt",print_r($args,true) . print_r($output,true) ,FILE_APPEND );
    }
}

The issue I am facing, the customer events are being called when the user register an account from the user interface. but when I add a new customer from the admin section it is not being triggered.

am I missing something here?


Solution

  • for admin part you should add event to admin/comtroller/extension/module/erp_integration.php this lines

    $this->model_setting_event->addEvent('customer_notification_add', 'admin/model/customer/customer/addCustomer/after', 'extension/module/erp_integration/addCustomer');
     $this->model_setting_event->addEvent('customer_notification_update', 'admin/model/customer/customer/editCustomer/after', 'extension/module/erp_integration/editCustomer');
    

    also do not forget in this file add this functions:

     // catalog/model/account/customer/addCustomer/after
        public function addCustomer(&$route, &$args, &$output)  {
            file_put_contents ( "testing.txt","\n******ADD CUSTOMER**********\n" ,FILE_APPEND );
            file_put_contents ( "testing.txt",print_r($args,true) . print_r($output,true) ,FILE_APPEND );
        }
    
        // catalog/model/account/customer/editCustomer/after
        public function editCustomer(&$route, &$args, &$output)  {
            //print_r($route); die;
            file_put_contents ( "testing.txt","\n******EDIT CUSTOMER**********\n" ,FILE_APPEND );
            file_put_contents ( "testing.txt",print_r($args,true) . print_r($output,true) ,FILE_APPEND );
        }
    

    Just note events for admin and catalog are separated. So if you need event in admin side, you must add events for admin...