1

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.

James Z
  • 12,209
  • 10
  • 24
  • 44
Mhand7
  • 527
  • 1
  • 3
  • 21

2 Answers2

1

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');
DigitCart
  • 2,980
  • 2
  • 18
  • 28
1

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

K. B.
  • 1,388
  • 2
  • 13
  • 25
  • I did create a new file under the catalog/controller/extension/module, with a file in the language folder, but I could not see it in the extension management screen under Modules in the Admin pages in-order to install it. but I will try it again to test if I missed something... – Mhand7 Apr 28 '20 at 09:24
  • You installing the all events at once in admin with existing your controller file. You not need to install it separate. Just place mentioned in my answer file. That's all. And try to edit an user. testing.txt will be created in the root of OC installation. – K. B. Apr 28 '20 at 09:26