Change order status for custom module

magento2magento2.4modulepayment-methodsPHP

I want to use one offline payment method module, I install this module

is there any way to change the status of the order to be Processing instead of Pending? Right now all my orders have status Pending and I really need to be Processing.

Thank you

Best Answer

  1. create app\code\Vendor\Extension\etc\events.xml with the following code.

    <?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_place_after">
             <observer name="vendor_sales_order_place_after" instance="Vendor\Extensoin\Observer\OrderObserver" />
        </event>
    </config>
    
  2. you need to create Observer file at following location.

    app\code\Vendor\Extension\Observer\OrderObserver.php
    
    
    <?php
    namespace Vendor\Extension\Observer;
    
    use Magento\Framework\Event\ObserverInterface;
    
    class OrderObserver implements ObserverInterface
    {
       public function execute(\Magento\Framework\Event\Observer $observer)
       {
         $order = $observer->getEvent()->getOrder();
         //NOW APPLY YOUR LOGIC HERE TO CHECK ORDER PAYMENT METHOD AND BASED ON THAT CHANGE THERE STATUS
       }
     }