handling Opencart-3 Events: AddCustomer EditCustomer

481 Views Asked by At

I am trying to handle events in Opencart-3 to move data to a local ERP system. I created an extension that contains all the events that I need to handle, My problem is that, only the events related to products are being triggered, but the others are not triggered.

<?php

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_add', 'admin/model/catalog/product/addProduct/after', 'extension/module/erp_integration/addProduct');
        $this->model_setting_event->addEvent('product_notification_update', 'admin/model/catalog/product/editProduct/after', 'extension/module/erp_integration/editProduct');

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

        $this->model_setting_event->addEvent('order_notification_add', '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_add');
        $this->model_setting_event->deleteEventByCode('product_notification_update');
        $this->model_setting_event->deleteEventByCode('customer_notification_add');
        $this->model_setting_event->deleteEventByCode('customer_notification_update');
        $this->model_setting_event->deleteEventByCode('order_notification_add');
    }

    // 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**********\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 );
    }
}

DO I need to create a new extension for the Catalog section (customer and product events), and register them in a different extension? Or what am I missing.

2

There are 2 best solutions below

2
On BEST ANSWER

For events which related to catalog in your case, you should create file in this folder. catalog/controller/extension/module named erp_integration.php with content:

<?php

class ControllerExtensionModuleErpIntegration extends Controller {
    private $error = array();
    // 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 );
    }

    // 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 );
    }
}

And your testing.txt you can find in the root of installation of the OC

0
On

Yes, For the Catalog events, You should create a new file in the catalog folder.

You have another mistake in your code (It is not related to this question):

Instead of using different names for each event, Use your extension name:

$this->model_setting_event->addEvent('erp_integration', ...
$this->model_setting_event->addEvent('erp_integration', ...

Then in the uninstall method you need to use deleteEventByCode once:

$this->model_setting_event->deleteEventByCode('erp_integration');