5

I've created a new offline payment module using this generator http://cedcommerce.com/magento-2-module-creator/payment-module

config.xml

.... <order_status>pending_payment</order_status> ....

system.xml

<field id="order_status" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="0"> <label>New Order Status</label><source_model>PackageName\ModuleName\Model\Config\Source\Order\Status\Pendingpayment</source_model> </field>

I'want to set new order status to pending or pending_payment, but it set to processing when creating a order every time.

0

first create observer into events.xml app\code\Adamsmage\Statuschange\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="Adamsmage\Statuschange\Observer\OrderObserver" />
</event>
</config>

second create Observer file at following location app\code\Adamsmage\Statuschange\Observer\OrderObserver.php

<?php
namespace Adamsmage\Statuschange\Observer;

use Magento\Framework\Event\ObserverInterface;

class OrderObserver implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
    $order = $observer->getEvent()->getOrder();
    // also if you want to check condition here like only offline payment method than order status update etc....
    $order->setState("pending")->setStatus("pending");
    $order->save(); 
}

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.