0

I have an Observer for checkout_onepage_controller_success_action and I would like to load ALL order information inside this observer: order items, options, customer info.

I have tried a number of different things, I will include them (commmented out) below.

<?php
namespace Company\Reports\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;

class ReportObserver implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer) {

    /*
    //doesn't work
     $order = $observer->getEvent()->getOrder();
     echo $orderId = $order->getId();
     echo "<pre>";
     print_r($order);
     echo "</pre>";
     */

     /*
     //doesn't work
     $orderId = $observer->getEvent()->getOrderIds();
      $order = $this->order->load($orderId);
      $orderid=$order->getEntityId();
      $custLastName= $orders->getCustomerLastname();
      $custFirsrName= $orders->getCustomerFirstname();
      $ipaddress=$order->getRemoteIp();
      $customer_email=$order->getCustomerEmail();
      $customerid=$order->getCustomerId();

      echo "<pre>";
      print_r($order);
      echo "</pre>";
      */

      /*
      //doesn't work
      $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
      $order = $objectManager->create('\Magento\Sales\Model\Order')->load($orderId);
      echo "<pre>";
      //print_r($order);
      echo "</pre>";
      echo "<hr>";
      echo "<pre>";
      print_r( $order->getAllItems() );
      echo "</pre>";
      */

      /*
      //doesn't work
     $order_ids = $observer->getEvent()->getOrderIds()[0];
     //$order = $this->_order->get($order_ids);
     $order = $this->order->get($order_ids);
     $order_id = $order->getIncrementId();
     $customer_email = $order->getCustomerEmail();
     foreach ($order->getAllVisibleItems() as $item)
     {
           $item->getName();
           $item->getSku();
           echo $item->getName();
     }
     */

     $orderIds = $observer->getEvent()->getOrderIds();
     echo $orderId = $orderIds[0];
     // can do sql queries here
     echo "<hr>";

     //print_r("order done");
     exit;
    }

}

3 Answers 3

1

ALL order information inside this observer:

app/code/VendoreName/ModuleName/etc/frontend

di.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="checkout_onepage_controller_success_action">
        <observer instance="VendoreName\ModuleName\Observer\ShoppingCart" name="shopping_cart_order_success"/>
    </event>
</config>

app/code/VendoreName/ModuleName/Observer

ShoppingCart.php

<?php

namespace VendoreName\ModuleName\Observer;

use Magento\Framework\Event\ObserverInterface;

class ShoppingCart implements ObserverInterface
{
    protected $cart;
    protected $orderRepository;

    public function __construct(
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
    ) {
        $this->cart = $cart;
        $this->orderRepository = $orderRepository;
    }
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        /* check  order id */
        $orderIds = $observer->getData('order_ids');
        if (is_array($orderIds)) {
            foreach ($orderIds as $orderIdKey => $orderIdVal) {
                $orderData = $this->orderRepository->get($orderIdVal);
                print_r($orderData->debug());
            }
        }
    }

}

I Hope This Helps You.

0

Please try with the below code.

<?php
namespace Company\Reports\Observer;

class ReportObserver implements \Magento\Framework\Event\ObserverInterface
{
  
    protected $_orderFactory;    
    
    public function __construct(        
        \Magento\Sales\Model\OrderFactory $orderFactory
    ) {
        $this->_orderFactory = $orderFactory;
    }

    /**
     * This is the method that fires when the event runs.
     *
     * @param \Magento\Framework\Event\Observer $observer
     */
    public function execute(\Magento\Framework\Event\Observer $observer )
    {               
        $orderIds = $observer->getEvent()->getOrderIds();
        
        if (count($orderIds)) {
            $orderId = $orderIds[0];            
            $order = $this->_orderFactory->create()->load($orderId);
            $orderid=$order->getEntityId();
            $custLastName= $order->getCustomerLastname();
            $custFirsrName= $order->getCustomerFirstname();
            $ipaddress=$order->getRemoteIp();
            $customer_email=$order->getCustomerEmail();
            $customerid=$order->getCustomerId();            
            $shippingAddress = $order->getShippingAddress();
            foreach($order->getAllVisibleItems() as $item)
             {
                //add code for fetch order item info here.
             }

             //For Full order data info remove comment below code and check the generated log file.
            // $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/order_data_'.date('my').'.log');
            // $logger = new \Zend\Log\Logger();
            // $logger->addWriter($writer);
            // $logger->info('---------- Full Order data ---------');
            // $logger->info(print_r($order->getData(), true));
        }
    }
}
7
  • thank you @jitendra. I just tried this, and as a quick test I added this: //add code for fetch order item info here. echo "<pre>"; print_r($item); echo "</pre>"; unfortunately it does not work... it seems like $order is just not available for some reason. it's empty. Commented Nov 2, 2021 at 18:26
  • can you uncomment logger code and comment out other code and check the $order->getData() in log file ? Commented Nov 2, 2021 at 18:54
  • Please adding above code please run below command. php bin/magento setup:di:compile Commented Nov 3, 2021 at 5:18
  • both of those things show that the order object is empty. no data in there. Commented Nov 3, 2021 at 14:58
  • Can you refer this link magento.stackexchange.com/questions/166695/… Commented Nov 3, 2021 at 15:22
0

Please Refer Below Code

<?php
namespace Company\Reports\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;

class ReportObserver implements ObserverInterface
{
    
    protected $order;  
  
    public function __construct(
    \Magento\Sales\Model\Order $order
    )
    {
        $this->order = $order;    
    }

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

    
      $order = $observer->getEvent()->getOrder();
      $orderId =$order->getId();  
      try {
    
        $order = $this->order->load($orderId);
        
        
        $orderid=$order->getEntityId();
        $custLastName= $order->getCustomerLastname();
        $custFirsrName= $order->getCustomerFirstname();
        $ipaddress=$order->getRemoteIp();
        $customer_email=$order->getCustomerEmail();
        $customerid=$order->getCustomerId();
        
        echo "<pre>";
        print_r($order->debug());
        echo "</pre>";
        die("exit");
      }catch (\Exception $e) {          
          $e->getMessage();
      }
    }

}
1
  • this gives an interesting error, I will look in to this: Argument 1 passed to Company\Reports\Observer\ReportObserver::__construct() must be an instance of Magento\Sales\Model\Order, instance of Magento\Sales\Model\OrderFactory given, Commented Nov 3, 2021 at 15:05

Your Answer

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

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