You can create a plugin for Before Place order action In Magento 2 using beforePlace() method.
When you required to check or set data before place order in Magento 2, You can use a plugin for OrderManagementInterface interface 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"?> <!-- /** * @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="before_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 declare(strict_types=1); /** * @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 beforePlace( OrderManagementInterface $subject, OrderInterface $order ): array { $quoteId = $order->getQuoteId(); if ($quoteId) { // your logic } return [$order]; } }
$subject contains the many functions you can explore the functions using get_class_methods($subject);
You can fetch $order object using the above way and based on your custom conditions you can get required data from the order object.
You can set data before place order using the above way.