Hey,
I'm trying to make a module that runs when an order is created, for that I think the best way is with the checkout_submit_all_after observer right?
I created a module it's enable with no problems at all but when I try to do a checkout it just don't complete.
events.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="checkout_submit_all_after"> <observer name="onorder_created" instance="Custom\onOrderCreate\Observer\onOrder" /> </event> </config>
Observer\onOrder.php
<?php namespace Custom\onOrderCreate\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Sales\Api\OrderRepositoryInterface; use Magento\Catalog\Model\ProductRepository; class onOrder implements OberserverInterface { public function __construct( OrderRepositoryInterface $OrderRepositoryInterface, ProductRepository $ProductRepository, \Magento\Customer\Model\Session $customerSession) { $this->orderRepository = $OrderRepositoryInterface; $this->ProductRepository =$ProductRepository; $this->_customerSession = $customerSession; } public function execute(\Magento\Framework\Event\Observer $observer ) { $order_ids = $observer->getEvent()->getOrderIds()[0]; var_dump($order_ids);die; $order = $this->orderRepository->get($order_ids); foreach ($order->getAllItems() as $item) { //code here } } }
for the observer I just used an example in other trend just to test it out. Even without the var_dump die it creates the order but in frontend just gives a red bar with ! (should have gone to a success page).
What am I doing wrong? There is problem that other modules use this same observer? Can I even use a var_dump there expecting a result?
Thanks
Solved! Go to Solution.
Hi @rui_silva1
You need to put your event.xml one of following location:
For Global: <module-dir>/etc/events.xml For Admin: <module-dir>/etc/adminhtml/events.xml For Frontend: <module-dir>/etc/frontend/events.xml
You can also refer below url:
https://devdocs.magento.com/guides/v2.3/ext-best-practices/extension-coding/observers-bp.html
Issue resolved ?
Please click on 'Kudos' & Accept as Solution!
Where did you put the events.xml? Directly in the etc/ directory of your module?
Hi @rui_silva1
You need to put your event.xml one of following location:
For Global: <module-dir>/etc/events.xml For Admin: <module-dir>/etc/adminhtml/events.xml For Frontend: <module-dir>/etc/frontend/events.xml
You can also refer below url:
https://devdocs.magento.com/guides/v2.3/ext-best-practices/extension-coding/observers-bp.html
Issue resolved ?
Please click on 'Kudos' & Accept as Solution!
Addendum:
For REST API: <module-dir>/etc/webapi_rest/events.xml
(Which is used the the One-Page-Checkout for example).
i am not able to see my obserser in frontend, not it redirecting to any other controller ,,...but it showing in the logs file