2

I have created a new observer to check the order data passing with "sales_order_place_before" event in observer.My observer is working but when I am trying to fetch the observer event data am getting null values.Please help me resolve this issue.Thanks in advance. My observer file

    namespace Custom\Order\Observer;

    use Magento\Framework\Event\ObserverInterface;

    class OrderPlacebefore implements ObserverInterface
    {
     protected $_objectManager;

      public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager
      ) {
          $this->_objectManager = $objectManager;
      }

      public function execute(\Magento\Framework\Event\Observer $observer)
      {     
        $event = $observer->getEvent();
        $orderObserverData = $observer->getEvent()->getData();
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info('mY obsever');
       $logger->info('data' . json_encode($orderObserverData));
      }
    }

2 Answers 2

1

To get the data from the event you need to make some small change in your code like below

public function execute(\Magento\Framework\Event\Observer $observer)
  {
        $event = $observer->getEvent();
        $orderObserverData = $$observer->getEvent()->getOrder()->getData();
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info('orderData' . json_encode($orderObserverData));

  }
0

Try following code:

  1. app/code/[VendorName]/[ModuleName]/etc/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="sales_order_place_before">
        <observer name="[VendorName]_[ModuleName]_sales_order_place_before" instance="[VendorName]\[ModuleName]\Observer\OrderPlacebefore"/>
    </event>
</config>
  1. app/code/[VendorName]/[ModuleName]/Observer/OrderPlacebefore.php
<?php
namespace [VendorName]\[ModuleName]\Observer;

use Magento\Framework\Event\ObserverInterface;

class OrderPlacebefore implements ObserverInterface
{
    protected $_objectManager;

    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager
    ) {
        $this->_objectManager = $objectManager;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $orderObserverData = $observer->getOrder()->getData();

        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info('OrderPlacebefore:');
        $logger->info('data' . json_encode($orderObserverData));
    }
}

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.