0

In Magento 2.3.x, I want to set the (custom) order status while customer placing an order. How to achieve this using event observer or plugin.

0

app\code\Vendor\Extension\etc\events.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_place_after">
      <observer name="vendor_sales_order_place_after" instance="Vendor\Extensoin\Observer\OrderObserver" />
   </event>
</config>

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)
     { 
        //Write your code here
     }   

}
0

You can use Plugin to do the same.

public function afterPlace(OrderObject $subject, $result)
{
    $result->setHoldBeforeState($result->getState());
    $result->setHoldBeforeStatus($result->getStatus());
    $result->setState(OrderObject::STATE_HOLDED)
               ->setStatus($subject->getConfig()
               ->getStateDefaultStatus(OrderObject::STATE_HOLDED));

    return $result;
}

and di.xml file

<?xml version="1.0" encoding="UTF-8" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Model\Order">
        <plugin name="order_flow_place_order_plugin"
                type="Company\OrderFlow\Plugin\Model\Order"/>
    </type>
</config>

I hope it will help you to get some basic idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.