cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 sales_order_save_after not returning order object based on order entity id

Magento 2 sales_order_save_after not returning order object based on order entity id

Event.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_save_after">
<observer name="magento_orderevent_create" instance="Magento\OrderEvent\Observer\OrderObserver" />
</event>
</config>

OrderObserver.php

 

<?php

namespace Magento\OrderEvent\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Message\ManagerInterface;

class OrderObserver implements ObserverInterface
{
/**
* @var ObjectManagerInterface
*/
protected $_objectManager;
protected $messageManager;

/**
* @param \Magento\Framework\ObjectManagerInterface $objectManager
*/
public function __construct(
\Magento\Framework\ObjectManagerInterface $objectManager,
\Magento\Quote\Model\QuoteFactory $quoteFactory,
\Magento\Sales\Model\Order $order,
ManagerInterface $messageManager
)
{
$this->_objectManager = $objectManager;
$this->messageManager = $messageManager;

}


public function execute(Observer $observer)
{
$order = $observer->getEvent()->getOrder();
$orderId = $order->getEntityId();

$customer = $order->getCustomerId();

$url = "http://captain-staging.us-east-2.elasticbeanstalk.com/api/v1/order/SaveSourceData?order_id=" . $orderId. "&customer_id=" . $customer;
echo($url);
// print_r("customer".$customer);
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => "http://captain-staging.us-east-2.elasticbeanstalk.com/api/v1/order/SaveSourceData?order_id=" . $orderId . "&customer_id=" . $customer,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 60,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_HTTPHEADER => array(
"Postman-Token: de7fe4a4-f86d-4c70-ae8a-1ba1d365b005",
"cache-control: no-cache"
),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
$error= "cURL Error #:" . $err;
echo $error;
exit();
throw new \Magento\Framework\Exception\CouldNotDeleteException(__($error));
} else {
print_r($response) ;
exit();
throw new \Magento\Framework\Exception\CouldNotDeleteException(__("Order is Successfully Saved!"));
}
}
}

 When i create an order i get order entity id and on that base in my API I retrieve complete order object in my API whenever a new order is created from admin or frontend

 

http://captain-staging.us-east-2.elasticbeanstalk.com/api/v1/order/SaveSourceData?order_id=22&custom...

 

Error

Client error: `GET http://qareeb-dev.us-east-2.elasticbeanstalk.com/rest/all/V1/orders/22` resulted in a `404 Not Found` response: {"message":"The entity that was requested doesn't exist. Verify the entity and try again."}

 
3 REPLIES 3

Re: Magento 2 sales_order_save_after not returning order object based on order entity id

Hello @muhammad ikram_ 

 

Use the below event to get the order data:

sales_model_service_quote_submit_before

To get order:

$order = $observer->getOrder();

Hope it helps.

---
If you've found my answer useful, please give"Kudos" and "Accept as Solution"

Re: Magento 2 sales_order_save_after not returning order object based on order entity id

sales_model_service_quote_submit_before

@Meetanshi  will this event will work on both front end and back end when the order is created  

Re: Magento 2 sales_order_save_after not returning order object based on order entity id

Hello, @muhammad ikram_ again!

 

Yes, this event will work on both front end and back end when the order is created.

---
If you've found my answer useful, please give"Kudos" and "Accept as Solution"