3

I just want to call an event observer when the event is dispatched via the REST API. Like if an order is placed from a mobile application, only then the sales_order_save_afterObserver should be called.

How do I write code for the same in events.xml? Or do we have to write somewhere else?

4

You have to place a events.xml in the webapi_rest area: app/[Vendor]/[Module]/etc/webapi_rest/events.xml

By placing the events.xml in the webapi_restdirectory the observer only listens to events dispatched in that area.

You can read about the areas and events in the devdocs.

| improve this answer | |
  • How can i print what event currently i am using into log file @Daniel – zus Aug 14 '19 at 9:18
  • @zus You'll need to inject \Psr\Log\LoggerInterface to write to the log. WIth $observer->getEventName() you can get the current event name. – Daniel Aug 14 '19 at 10:08
  • For magento 1, how can i do that? – zus Aug 14 '19 at 10:09
  • here is my code, not working paste.ofcode.org/UuYfuMtnh84raJux5rUF8A – zus Aug 14 '19 at 10:16
  • Sorry I don't know for Magento 1. You might ask a separate question for this. – Daniel Aug 14 '19 at 10:35
4

You can declare your observer at the following location to call it via the REST API only.

app/code/Vendor/Module/etc/webapi_rest/events.xml

| improve this answer | |
  • How can i print what event currently i am using into log file @Dinesh – zus Aug 14 '19 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.