My requirement is to change order status when an order is placed successfully in Magento 2 .what is the event can I use so that I can change order status ,I have tried sales_order_save_after event but it will triggers whenever an order is saved ,I can't use this event and my orders are placed through cron also I mean if I use checkout_submit_all_after event it will only triggers only from frontend .can I know what is the best event to do this job.please suggest me .
1 Answer
Try the event
checkout_submit_all_after
It runs after order is submitted successfully (both frontend and backend)
For example, in events.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="checkout_submit_all_after">
<observer name="{vendor}_{module}_checkout_submit_all_after" instance="{Vendor}\{Module}\Observer\CheckoutSubmitAllAfter" />
</event>
</config>
And in the Observer
namespace {Vendor}\{Module}\Observer;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
class CheckoutSubmitAllAfter implements ObserverInterface
{
public function execute(EventObserver $observer)
{
$order = $observer->getOrder();
$quote = $observer->getQuote();
// Enter your code here
return $this;
}
}
-
Thank You For your answer , but this event triggers only when the order is placed using checkout in frontend, but for me orders are placed through crons also.We have subscription products , for first time when customer places an order for subscription products it will placed through frontend but for second time it will placed through crons , so checkout event won't work.– PrasanthJun 24, 2019 at 17:57
-
checkout_submit_all_after
is not firing mean you don't create order right way at cron job– Amit Bera ♦Jun 24, 2019 at 18:17