You can create a plugin for after Place order action in Magento 2 using afterPlace() plugin method.
When you required to check or get data after place order in Magento 2 or save data to other tables after place order successfully You can use afterPlace plugin from OrderManagementInterface interface’s place() method.
place() method from the Magento\Sales\Api\OrderManagementInterface interface class used to Place order operation in Magento 2 from checkout page.
You just need to define plugin definition in di.xml file to create a plugin.
Create a Global di.xml file inside etc/di.xml file,
<?xml version="1.0"?> <!-- /** * Dependency injector xml file * * @author Rakesh Jesadiya */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Sales\Api\OrderManagementInterface"> <plugin name="after_place_order_operation" type="Rbj\Order\Plugin\OrderManagement"/> </type> </config>
Now you need to create OrderManagement.php file inside your module Plugin folder.
Path: app\code\Rbj\Order\Plugin\OrderManagement.php
<?php /** * @author Rakesh Jesadiya */ namespace Rbj\Order\Plugin; use Magento\Sales\Api\Data\OrderInterface; use Magento\Sales\Api\OrderManagementInterface; /** * Class OrderManagement */ class OrderManagement { /** * @param OrderManagementInterface $subject * @param OrderInterface $order * * @return OrderInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterPlace( OrderManagementInterface $subject, OrderInterface $result ) { $orderId = $result->getIncrementId(); if ($orderId) { // your logic } return $result; } }
You can fetch $result object using the above way and you can set your custom conditions.
You can get required data from the result object for an order.