Post by cbaldoni » Fri Feb 25, 2022 2:47 am

Hi there, I'm trying to intercept the creation of a new customer, commanded through restadmin API . Below is what I implemented (using events). The event handler works fine when I add a customer trhough admin client panel. But it is not triggered when the customer is generated via Rest API (as a matter of fact through POST I call https://myopencartshopURL/store/index.p ... /customers). What am I doing wrong? My doubt is that restadmin doesn't trigger events ??? . Any help? How else could I intercept the event (beside modifying the catalog\controller\rest\customer_admin.php source)? Thanks for helping!! :joker:

Code: Select all

class ControllerExtensionModuleCustomergroupmodifier extends Controller {
	private $error = array();
	public function index() {...}
	protected function validate() {}

public function install()
  {
    $this->load->model('setting/event');
    $this->model_setting_event->addEvent(
      'module_customergroupmodifier_admin',
      'admin/model/customer/customer/addCustomer/before',
      'extension/module/customergroupmodifier/addCustomer'
    );
    $this->model_setting_event->addEvent(
      'module_customergroupmodifier_restadmin',
	  'catalog/model/rest/restAdmin/addCustomer/before',
      'extension/module/customergroupmodifier/addCustomer'
    );
	$this->log->write('module installed!');
  }

  public function uninstall()
  {
    $this->load->model('setting/event');
    $this->model_setting_event->deleteEventByCode(
      'module_customergroupmodifier_admin'
    );
    $this->model_setting_event->deleteEventByCode(
      'module_customergroupmodifier_restadmin'
    );
	$this->log->write('module uninstalled!');
  }
     public function addCustomer(&$eventRoute, &$data)  {
			foreach ($data[0] as $key => $value) {
					if (is_array($value)) $value = json_encode($value);
					$this->log->write($key . '=' . $value);
			}
         }
}
Last edited by cbaldoni on Wed Mar 02, 2022 4:28 am, edited 1 time in total.

Newbie

Posts

Joined
Fri Feb 25, 2022 2:26 am

Post by paulfeakins » Fri Feb 25, 2022 11:26 pm

cbaldoni wrote:
Fri Feb 25, 2022 2:47 am
How else could I intercept the event (beside modifying the catalog\controller\rest\customer_admin.php source)?
vQmod or OCMOD.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Legendary Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by straightlight » Sat Feb 26, 2022 2:26 am

cbaldoni wrote:
Fri Feb 25, 2022 2:47 am
Hi there, I'm trying to intercept the creation of a new customer, commanded through restadmin API . Below is what I implemented (using events). The event handler works fine when I add a customer trhough admin client panel. But it is not triggered when the customer is generated via Rest API (as a matter of fact through POST I call https://myopencartshopURL/store/index.p ... /customers). What am I doing wrong? My doubt is that restadmin doesn't trigger events ??? . Any help? How else could I intercept the event (beside modifying the catalog\controller\rest\customer_admin.php source)? Thanks for helping!! :joker:
class ControllerExtensionModuleCustomergroupmodifier extends Controller {
private $error = array();
public function index() {...}
protected function validate() {}

public function install()
{
$this->load->model('setting/event');
$this->model_setting_event->addEvent(
'module_customergroupmodifier_admin',
'admin/model/customer/customer/addCustomer/before',
'extension/module/customergroupmodifier/addCustomer'
);
$this->model_setting_event->addEvent(
'module_customergroupmodifier_restadmin',
'catalog/model/rest/restAdmin/addCustomer/before',
'extension/module/customergroupmodifier/addCustomer'
);
$this->log->write('module installed!');
}

public function uninstall()
{
$this->load->model('setting/event');
$this->model_setting_event->deleteEventByCode(
'module_customergroupmodifier_admin'
);
$this->model_setting_event->deleteEventByCode(
'module_customergroupmodifier_restadmin'
);
$this->log->write('module uninstalled!');
}
public function addCustomer(&$eventRoute, &$data) {
foreach ($data[0] as $key => $value) {
if (is_array($value)) $value = json_encode($value);
$this->log->write($key . '=' . $value);
}
}
}
- The index() is used as a method but the default route is not used in the database
- $eventRoute, I have no idea where this is originating from.

Documentation: https://github.com/opencart/opencart/wiki/Events-System

In addition, you could also use the debug event controller file to run these tests.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by cbaldoni » Sun Feb 27, 2022 12:30 am

Hi, thanks for your replies. @paulfeakins I know I could modify the behaviour by OCMOD, but I'm trying to avoid that using events (which I never used so far :-)).
@straightlight, $eventRoute when the event is correctly triggered by adding a new user through admin panel gives:customer/customer/addCustomer. My understanding is that, as it is brought as a function argument with the ampersend, through the code in extension/module/customergroupmodifier/addCustomer you could be able to re-route the return to a different endpoint (e.g. another html page or template), just modifying programmatically $eventRoute value.

Any idea on how to intercept events called by restadmin methods? Is that possible? In the meanwhile I'll have a look to https://github.com/opencart/opencart/wiki/Events-System . Thanks to all, cbaldoni

Newbie

Posts

Joined
Fri Feb 25, 2022 2:26 am

Post by straightlight » Mon Feb 28, 2022 12:54 am

cbaldoni wrote:
Sun Feb 27, 2022 12:30 am
Hi, thanks for your replies. @paulfeakins I know I could modify the behaviour by OCMOD, but I'm trying to avoid that using events (which I never used so far :-)).
@straightlight, $eventRoute when the event is correctly triggered by adding a new user through admin panel gives:customer/customer/addCustomer. My understanding is that, as it is brought as a function argument with the ampersend, through the code in extension/module/customergroupmodifier/addCustomer you could be able to re-route the return to a different endpoint (e.g. another html page or template), just modifying programmatically $eventRoute value.

Any idea on how to intercept events called by restadmin methods? Is that possible? In the meanwhile I'll have a look to https://github.com/opencart/opencart/wiki/Events-System . Thanks to all, cbaldoni
It is not brought as a function. It is brought as a method using references from the Engine which the startup looks up based on the active event initiated from the database. As for the restadmin, you'd need to provide a more detailed e.g on what you're trying to achieve.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by cbaldoni » Mon Feb 28, 2022 5:27 pm

@Straightlight, thanks for supporting. I'll try to explain my goal. I've an application external to OC, which interacts with my OC instance in some circumstances. One of these is the event of a new customer registering to my application. If the registration is successfull, the new customer data are mirrored as valid credentials in OpenCart, i.e. a new OpenCart customer is created through restadmin by rest POST call (/index.php?route=rest/customer_admin/customers). I need to intercept such call, before the customer is actually inserted in the DB, to modify the customer_group_id programmatically (based on some info passed to OC through custom_field). I presume I could easily achieve such result by directly modifying addCustomer method in php file catalog/controller/rest/customer_admin method addCustomer (via OCMOD). But as recommended by OC gurus, I'd better do it through event manager. Below is the lines of code which would be trigger in the event (before). The code is working fine when the event is triggered by 'admin/model/customer/customer/addCustomer/before', but apparently is not engaged neither by 'catalog/model/rest/restadmin/addCustomer/before',
nor by 'catalog/controller/rest/customer_admin/addCustomer/before'.

// when inserting a new customer, check custom_field attributes to match appropriate customer_group_id which the new user belongs to
public function addCustomer(&$eventRoute, &$data) {

if ($data[0]['custom_field']['14'] == '14' && $data[0]['custom_field']['8'] == '9'){
//Juridical persons (14:14) UE resident (8:9);
$this->log->write('a new UE juridic person is being added');
//setting customer group id to juridicpersonUE;
$data[0]['customer_group_id'] = 3;
}

if ($data[0]['custom_field']['14'] == '14' && $data[0]['custom_field']['8'] == '8'){
//Juridical persons (14:14) XE resident (8:8);
$this->log->write('a new Extra UE juridic person is being added');
//setting customer group id to juridicpersonXE;
$data[0]['customer_group_id'] = 4;
}
}

Newbie

Posts

Joined
Fri Feb 25, 2022 2:26 am

Post by straightlight » Tue Mar 01, 2022 2:14 am

cbaldoni wrote:
Mon Feb 28, 2022 5:27 pm
@Straightlight, thanks for supporting. I'll try to explain my goal. I've an application external to OC, which interacts with my OC instance in some circumstances. One of these is the event of a new customer registering to my application. If the registration is successfull, the new customer data are mirrored as valid credentials in OpenCart, i.e. a new OpenCart customer is created through restadmin by rest POST call (/index.php?route=rest/customer_admin/customers). I need to intercept such call, before the customer is actually inserted in the DB, to modify the customer_group_id programmatically (based on some info passed to OC through custom_field). I presume I could easily achieve such result by directly modifying addCustomer method in php file catalog/controller/rest/customer_admin method addCustomer (via OCMOD). But as recommended by OC gurus, I'd better do it through event manager. Below is the lines of code which would be trigger in the event (before). The code is working fine when the event is triggered by 'admin/model/customer/customer/addCustomer/before', but apparently is not engaged neither by 'catalog/model/rest/restadmin/addCustomer/before',
nor by 'catalog/controller/rest/customer_admin/addCustomer/before'.

// when inserting a new customer, check custom_field attributes to match appropriate customer_group_id which the new user belongs to
public function addCustomer(&$eventRoute, &$data) {

if ($data[0]['custom_field']['14'] == '14' && $data[0]['custom_field']['8'] == '9'){
//Juridical persons (14:14) UE resident (8:9);
$this->log->write('a new UE juridic person is being added');
//setting customer group id to juridicpersonUE;
$data[0]['customer_group_id'] = 3;
}

if ($data[0]['custom_field']['14'] == '14' && $data[0]['custom_field']['8'] == '8'){
//Juridical persons (14:14) XE resident (8:8);
$this->log->write('a new Extra UE juridic person is being added');
//setting customer group id to juridicpersonXE;
$data[0]['customer_group_id'] = 4;
}
}
Modifying with OCMod with the new path: rest would not be detected by the OC admin extension installer, in this case. This new path would be ignored.

However, the code you have posted on the above is partial. You could also try using this API: viewtopic.php?f=202&t=220903#p805005 or using an API extension from the Marketplace. In addition, you could also create a new service request in the Commercial Support section of the forum to get this done as a custom job.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by cbaldoni » Wed Mar 02, 2022 12:16 am

Dear all. I finally found the solution!! exactly here: viewtopic.php?t=224686#p824511.
User memberlist.php?mode=viewprofile&u=614071 faced exactly my problem and he finally found out: when dealing with events managed within the forefront office (i.e. customer side or restadmin calls), you need to create your event controller extension in the catalog tree.

My code is now working perfectly.
I just had to move everything under the catalog section.
I think this should be better explained in the reference documentation.

Additionally, using OC 3.0.3.2 I had to follow the entire extension approach: i.e.
* 2 mycontroller.php under catalog (1 under module, 1 under language)
* mycontroller.twig (under view);

* the same under admin: 1 mycontrollerphp.php
* 1 mycontroller.twig).

By the admin controller I install/uninstall the events and validate the extension module, playing with extension module installation (through admin extension panel).
In the catalog controller I've a basically an empty index() and the methods which are triggered when the events are fired. Is a little bit cumbersome.... but finally it works!!!

Thanks, Cristiano

Newbie

Posts

Joined
Fri Feb 25, 2022 2:26 am

Post by straightlight » Wed Mar 02, 2022 12:20 am

cbaldoni wrote:
Wed Mar 02, 2022 12:16 am
Dear all. I finally found the solution!! exactly here: viewtopic.php?t=224686#p824511. User memberlist.php?mode=viewprofile&u=614071 faced exactly my problem and he finally found out: when dealing with events managed within the forefront office (i.e. customer side or restadmin calls), you need to create your event controller extension in the catalog tree. My code is now working perfectly. I just had to move everything under the catalog section. I think this should be better explained in the reference documentation. Additionally, using OC 3.0.3.2 I had to follow the entire extension approach: i.e. 2 mycontroller.php under catalog (1 under module, 1 under language) + mycontroller.twig (under view); the same under admin (2 mycontrollerphp.php+ 1 mycontroller.twig). By the admin controller I install/uninstall the events and validate the extension module, playing with extension module installation (through admin extension panel). In the catalog controller I've a basically an empty index() and the methods which are triggered when the events are fired. Is a little bit cumbersome.... but finally it works!!! Thanks, Cristiano
https://github.com/opencart/opencart/wi ... -an-action . Already addressed. Now that the issue has been solved on your own, please add: [SOLVED] at the beginning of the subject line on your first post.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by cbaldoni » Wed Mar 02, 2022 4:31 am

Thanks. @straightlight: frankly speaking, I think you could improve the documentation at https://github.com/opencart/opencart/wiki/Events-System
the topic is complex but indeed developpers can do a lot with events, once you learn to manage them.

Newbie

Posts

Joined
Fri Feb 25, 2022 2:26 am

Post by straightlight » Wed Mar 02, 2022 4:50 am

cbaldoni wrote:
Wed Mar 02, 2022 4:31 am
Thanks. @straightlight: frankly speaking, I think you could improve the documentation at https://github.com/opencart/opencart/wiki/Events-System
the topic is complex but indeed developpers can do a lot with events, once you learn to manage them.
Thanks for the feedback. However, it was intended to keep the documentation on a momentum level prior to expand the knowledge until forum users will, or may never, get fully familiarized with it.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON
Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot], muko34 and 20 guests