0

I've got a custom payment module. How can I update the order status to complete on the action of place order? If the order is placed choosing the custom payment method.

Thanks in advance!

0

You can use event observer for changing status of an order:

sales_order_place_after

You can visit below link:

0

you can use events: sales_order_save_after and sales_order_state_change_before. I refer sales_order_save_after event.

In our observer, we can get the state of order:

Observer/SalesOrderAfterSave.php

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $order = $observer->getEvent()->getOrder();
    if ($order instanceof \Magento\Framework\Model\AbstractModel) {
       if($order->getState() == 'canceled' || $order->getState() == 'closed') {
            //Your code here
       }
    }
    return $this;
}

you can read more about observer : Magento 2 events list

NOTE: you should try with Plugin:

etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="Magento\Sales\Api\OrderRepositoryInterface">
        <plugin name="order_state_plugin"
                type="Company\Module\Model\Plugin\OrderStatePlugin"/>
    </type>

</config>

Plugin:

<?php

namespace vendor_name\module_name\Plugin;

class file.name
{
/**
 * @param \Magento\Sales\Api\OrderRepositoryInterface $subject
 * @param \Magento\Sales\Api\Data\OrderInterface $result
 * @return mixed
 * @throws \Exception
 */
public function afterSave(\Magento\Sales\Api\OrderRepositoryInterface $subject,$result)
     {
    if($result->getState() == Order::STATE_COMPLETE) {
        ......
    }
    return $result;

}

accept if helps

  • Hi, I need to update order status complete, If the custom payment method is selected. – Garry Nov 18 at 12:10
  • create a condition on custom method like if(method_select) { update order status as above } – Asad Ullah Nov 18 at 12:40
  • What will be the path for this plugin? – Garry Nov 18 at 12:42
  • I have custom module. – Garry Nov 18 at 12:43
  • 1
    you can create it like app/code/vendor_name/module_name/Plugin/file.name – Asad Ullah Nov 18 at 12:45

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.