In magento i have to get order items in observer

I am trying two method sales_order_place_after and checkout_submit_all_after

Both event is not working

my events.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="checkout_cart_product_add_after">
    <observer name="cart_product" instance="Example\Details\Observer\CartProduct" />
<event name="checkout_submit_all_after">
    <observer name="oreder_item" instance="Example\Details\Observer\OrderItem" />

my OrderItem.php


namespace Example\Details\Observer;
use Magento\Framework\Event\ObserverInterface;
class OrderItem implements ObserverInterface
public function __construct(
    \Magento\Framework\App\Request\Http $request,
    \Magento\Framework\ObjectManagerInterface $objectManager
) {
    $this->_request = $request;
    $this->_objectManager = $objectManager;

public function execute(\Magento\Framework\Event\Observer $observer)

     $orderItems = $observer->getOrder();


I use both condition one by one but its not working

  • From your above code how can you tell that it is not working? Have you tried putting logging in the class? – Smartie Feb 3 '16 at 12:07
  • @Smartie i am enter order id in my custom module but its not working – Newbie Feb 5 '16 at 7:20

Your way of implementation is wrong. You need to observe the event under your module's config.xml file. As mentioned below,

Under your module etc/config.xml


Under Model/Observer.php

class NAMESPACE_MODULE_Model_Observer {

    public function getorderDetails($observer){
        $order = $observer->getEvent()->getOrder();     
        $billing = $order->getBillingAddress();
        $shipping = $order->getShippingAddress();
        $customer = $order->getCustomer();         
        $order_id = $order->getIncrementId();
        $customer_id = $order->getCustomerId();         
        return true;


Replace NAMESPACE with your Package name and MODULE with your Module name.

Hope this helps.

  • Question is about "Magento 2". Are you sure this method is still valid ? – Prateek Feb 3 '16 at 12:49
  • @Prateek No it wont. I didn't see the magento2 tag. – Vinoth Feb 3 '16 at 13:02
  • @Vinoth I am using Magento 1, how to print what event using in observer and print into log-file, just print event in log-file, how can i do that? – zus Aug 14 at 9:15

You can use

<event name="checkout_onepage_controller_success_action">
        <observer name="after_place_order" instance="namespace\module\model\Observer"/>

in obsereve

namespace namespace\module\model\Observer;

use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Catalog\Model\ProductRepository;

class Observer implements \Magento\Framework\Event\ObserverInterface {

    protected $orderRepository;
    protected $ProductRepository;
    protected $customerSession;

    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];

        $order = $this->orderRepository->get($order_ids);
        foreach ($order->getAllItems() as $item) {
            //code here

  • hi @sagar Dobariya let me know how can save custom field data here? could u tell me thanks – NIPPU Jun 29 '17 at 7:12

No need to change any event just adds the log in the execution file. 
Like this.


<event name="sales_order_place_after">
    <observer name="oreder_item" instance="Example\Details\Observer\OrderItem" / >

And your Observer add the log like this. OrderItem.php

public function execute(\Magento\Framework\Event\Observer $observer)
    $order = $observer->getEvent()->getOrder();

    $orderId = $order->getId();

    $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/testingObserver.log');

    $logger = new \Zend\Log\Logger();




After placing the order please checks your log you will see the order ID.

Kay K

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.