0

I am not able to get order details in observer using sales_order_save_before event:-

Path: - /app/code/Company/Mymodule/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="catalog_category_delete_after">
        <observer name="Mymodule" instance="Company\Mymodule\Observer\Onafterdelete"/>
    </event>
    <event name="sales_order_save_before">
        <observer name="Mymodule" instance="Company\Mymodule\Observer\Onbeforesaveorder"/>
    </event>
</config>

Path:- /app/code/Company/Mymodule/Observer/Onbeforesaveorder.php

class Onbeforesaveorder implements \Magento\Framework\Event\ObserverInterface
{
    protected $_resource;

    /**
    * @var \Psr\Log\LoggerInterface
    */
   protected $_logger;  

    public function __construct(
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\App\ResourceConnection $resource
    ) {
        $this->_logger = $logger;
        $this->_resource = $resource;
    }  

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

    if($order->getId()){
        $this->_logger->debug($order->getEntityId()." observer1111");
    }

    if ($orderObj->getId()){
        $this->_logger->debug($orderObj->getEntityId()." observer22222");
    }

    $this->_logger->debug("observer3333");// event is fired because I am getting this message in my log file.
    return $this;
  }
}

I am trying two way to get order ID in my code but both way is not working.

Please let me know if I am missing any thing or doing wrong as event is fired but not able to get order details in observer.

0

You should use the checkout_submit_all_after event. The event which you are using will not have order data as order will not be created by then.

|improve this answer|||||
0

Company\Mymodule\etc\events.xml

<event name="sales_model_service_quote_submit_before">
    <observer name="getorderinfo" instance="Company\Mymodule\Observer\Onbeforesaveorder"/>
</event>

In observer file,

use Magento\Framework\Event\Observer;
use Company\Mymodule\Model\Transaction;

class Onbeforesaveorder extends AbstractObserver
{
    public function execute(Observer $observer)
    {
        /** @var \Magento\Sales\Model\Order $order */
        $order = $observer->getEvent()->getOrder();
        $address = $observer->getEvent()->getAddress();        
    }
}
|improve this answer|||||
0

Write a below code to Company\Mymodule\etc\events.xml

sales_model_service_quote_submit_before

You can get order id by below code

echo "<pre>";
print_r($observer->getOrder()->getIncrementId());
exit;
|improve this answer|||||
  • Thanks for your reply but Not working – Purushotam Sharma Feb 8 '18 at 11:18
  • Check my updated answer – Suresh Chikani Feb 8 '18 at 11:24
  • I have did the same – Purushotam Sharma Feb 8 '18 at 11:30
  • Check my updated answer, you will get order id. – Suresh Chikani Feb 8 '18 at 11:33
  • I am able to get Increment Id but not order id, How to get order is here? – Purushotam Sharma Feb 8 '18 at 11:47
0

Try this code

<?php
namespace modulename\frontname\Observer;

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

class OrderPlace implements ObserverInterface
{
    public function __construct(
        \Magento\Sales\Api\Data\OrderInterface $order
    ) {

        $this->order = $order;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $orderids = $observer->getEvent()->getOrderIds();
        foreach ($orderids as $orderid) {
            $order = $this->order->load($orderid);
        }

        $storeId = $order->getStoreId();
        $oldStatus = $order->getOrigData('status');
        $newStatus = $order->getData('status');\

        }

}
|improve this answer|||||

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.