cancel
Showing results for 
Search instead for 
Did you mean: 

sales_order_save_after observer is not getting order entity id

SOLVED

sales_order_save_after observer is not getting 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->getId();
echo($orderId);exit();
$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 orderId empty what should i do i want to trigger my api whenever new order is created from admin or frontend

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

{"error":[{"field":"order_id","message":"The order id field is required."}],"statusCode":false}

1 ACCEPTED SOLUTION

Accepted Solutions

Re: sales_order_save_after observer is not getting order entity id

Try with EntityId:

    /**
     * @param Observer $observer
     */
    public function execute(Observer $observer)
    {
        /** @var OrderInterface $order */
        $order = $observer->getEvent()->getOrder();
        $orderId = $order->getEntityId();
        echo "<pre>"; print_r($orderId);exit;
    }

 and use $order variable as OrderInterface Object.

 

Hope it helps!

 

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

View solution in original post

3 REPLIES 3

Re: sales_order_save_after observer is not getting order entity id

Try with EntityId:

    /**
     * @param Observer $observer
     */
    public function execute(Observer $observer)
    {
        /** @var OrderInterface $order */
        $order = $observer->getEvent()->getOrder();
        $orderId = $order->getEntityId();
        echo "<pre>"; print_r($orderId);exit;
    }

 and use $order variable as OrderInterface Object.

 

Hope it helps!

 

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

Re: sales_order_save_after observer is not getting order entity id

Now it says 

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."}

Re: sales_order_save_after observer is not getting order entity id

I think you are trying to call wrong API, try with  http://qareeb-dev.us-east-2.elasticbeanstalk.com/rest/V1/orders/22