5

i want to get the order event and want to use the order object data , tried this but something is missing, here is what i tried and how can i use this data in my helper/block class or controller action?

etc/adminhtml/events.xml

<?xml version="1.0"?>


<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

    <event name="test_order">
        <observer name="afterOrderObserver" instance="Vendor\Module\Observer\Order"/>
    </event>
</config>

Observer/TestOrder.php

<?php

namespace Vendor\Module\Model\Observer;

use Magento\Framework\Event\ObserverInterface;

class TestOrder implements ObserverInterface
{
    /**
     * Order Model
     *
     * @var \Magento\Sales\Model\Order $order
     */
    protected $order;

     public function __construct(
        \Magento\Sales\Model\Order $order
    )
    {
        $this->order = $order;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
       $orderId = $observer->getEvent()->getOrderIds();
        $order = $this->order->load($orderId);

        //get Order All Item
        $itemCollection = $order->getItemsCollection();
        $customer = $order->getCustomerId(); // using this id you can get customer name

    }
}

5 Answers 5

5

Please modify your code like below

    <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

   <event name="sales_order_place_after">
       <observer name="order_data" instance="Vendor\Module\Observer\OrderData" />
   </event>
</config>

OrderData.php

class OrderData implements \Magento\Framework\Event\ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
     $order = $observer->getEvent()->getOrder();
     echo $order->getId();
     exit;
    }
}

make sure you place events.xml under etc/adminhtml let me know if any help needed.

1
  • thanks for pointing out the mistake,one another mistake of namespace, namespace should be namespace Vendor\Module\Observer; instead of Vendor\Module\Model\Observer; Nov 12, 2018 at 9:33
3

For me, sales_order_save_commit_after was the only event that worked well:

  • If you need the event to be triggered in the admin area and also in the frontend, you can add the following code in etc/events.xml.
  • If you need it for the order created from admin only, you need to add the following code in etc/adminhtml/events.xml

Code:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_order_save_commit_after">
        <observer name="order_data" instance="Vendor\Module\Observer\OrderData" />
    </event>
</config>
1
  • of all the above, this one really works for me on getting the exact order data from the database
    – fmsthird
    Sep 27, 2019 at 2:30
1

Event name tag indicates the name of the event you want to observe

In your case you'd take a look at vendor/magento/module-sales/Model/Order.php class, in concrete place() method. You'll see 2 events fired there

public function place()
{
    $this->_eventManager->dispatch('sales_order_place_before', ['order' => $this]);
    $this->_placePayment();
    $this->_eventManager->dispatch('sales_order_place_after', ['order' => $this]);
    return $this;
}

If you want to run your code after order has been placed, then you should change

<event name="test_order">

And use

<event name="sales_order_place_after">

Note that the object attached to this event is $order, you should be able to access it in your observer using

$order = $observer->getEvent()->getOrder();
3
  • How can i print what event currently i am using into log file @Raul
    – zus
    Aug 14, 2019 at 9:19
  • Hi there, you'd ask this in a separate question better, so people can answer there ;) Aug 14, 2019 at 10:30
  • yes i posted, magento.stackexchange.com/q/285292/57334
    – zus
    Aug 14, 2019 at 10:31
0

I'm finding that and Observer for sales_order_place_after and code -

$order = $observer->getEvent()->getOrder();
$orderID = $order->getId();

Returns a null orderID, although the event does return an order.

As per the rules of the Observer, the event.xml goes into neither the frontend nor the adminhtml directories since this event is fired from both areas. I did (just for fun)try placing the event in adminhtml and then frontend, but it didn't trigger the observer.

I'm thinking that the orderid is not yet assigned at this point, as It's not the call to ->orderId() that is failing, for in the debugger the order object itself has a null orderid.

Anyone else run into this? I feel that its a timing issue?

One answer is to use the checkout_submit_all_after event. But, I believe this only is fired from the One Page Checkout, so be aware.

2
  • I got the same problem. Did you solve it?
    – ian
    Jun 25, 2019 at 11:28
  • I ended up using the checkout_submit_all_after event. Aug 1, 2019 at 21:40
0

In 2.3.4, I found that using sales_order_place_after was not useful because it was called before the order was given an ID.

I found that sales_model_service_quote_submit_success can be observed for both frontend and admin orders.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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