0

I have integrated Custom payment module. I want to save every order placed with custom payment selected should be set to complete.

( If the selected payment mode is with MYPay ) Order Status -> complete

I have tried like this vendor_name\module_name\etc\di.xml

di.xml file

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Checkout\Model\DefaultConfigProvider" type="Iostpay\Iostpaymagento\Model\DefaultConfigProvider" />
     <type name="Magento\Sales\Api\OrderRepositoryInterface">
        <plugin name="order_state_plugin"
                type="Iostpay\Iostpaymagento\Model\Plugin\OrderStatePlugin"/>
    </type>
</config>

Plugin

vendor_name\module_name\Plugin\OrderStatePlugin.php
<?php

namespace vendor_name\module_name\Plugin;

class OrderStatePlugin
{
/**
 * @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;

 }

But If this is the only way to do. what do I need to update in the plugin Code?

Does anyone know how to do this? Thanks in Advance.

  • If you want to complete that order then, you need to create Invoice and Shipment for current order first programmatically then after this order will complete automatically. – Kishan Savaliya Nov 19 at 6:08
  • Can i set the order status to processing or complete by default? – Garry Nov 19 at 6:14
  • Please check this answer magento.stackexchange.com/questions/164600/… – Kishan Savaliya Nov 19 at 6:18
  • I went through this before, but can't find the way to do this. I mean what file should i code and what to code. – Garry Nov 19 at 6:26
  • You can create observer event for that 'sales_order_place_after' and check if order placed through your payment method then you can change status of current order. – Kishan Savaliya Nov 19 at 6:27
1

You need to create one events.xml file here in your custom module

app/code/Vendor/Module/etc/events.xml

Content for this file is..

<?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="checkout_submit_all_after">
        <observer name="set_processing_order_place_after" instance="Vendor\Module\Observer\OrderPlaceAfter" />
    </event>
</config>

Here we have used checkout_submit_all_after event so this will call when order successfully placed in Magento. So now we need to create one Observer file here

app/code/Vendor/Module/Observer/OrderPlaceAfter.php

Content for this file is..

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;

class OrderPlaceAfter implements ObserverInterface
{
    public function execute(Observer $observer){
        try {
            $order = $observer->getEvent()->getOrder();
            if($order->getPayment()->getMethod() == 'iostpay'){
                $orderState = \Magento\Sales\Model\Order::STATE_PROCESSING;
                $order->setState($orderState)->setStatus(\Magento\Sales\Model\Order::STATE_PROCESSING);
                $order->save();
            }
        } catch (\Exception $e) {
            $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/OrderPlaceAfterException.log');
            $logger = new \Zend\Log\Logger();
            $logger->addWriter($writer);
            $logger->info($e->getMessage());
        }
    }
}

Here I've changed Status of order from pending to processing after successfully placed order. You can change condition based on your requirement here. Also I've added one condition there, if Payment method is iostpay then only we will change order status otherwise not.

Hope this will work for you!

  • Let me know if it helps @Garry. – Kishan Savaliya Nov 19 at 7:14
  • 1
    Great.. this helped.. Thanks.. One more thing what would be the order status code for complete order? – Garry Nov 19 at 7:25
  • 1
    You can check list of order status const variable in this file vendor/magento/module-sales/Model/Order.php and for COMPLETE status you can use STATE_COMPLETE – Kishan Savaliya Nov 19 at 7:27
0

STEP 1 : as i have mentioned before you need to get current order status here using its API or MODEL by providing DI in the file like e.g

 $order->getStatus(); 

SETP 2 : when you get the current status update status to what ever you want like e.g

$order->setStatus('complete'); 

STEP 3 : now you have to save the order in order to save your current changing in your order e.g

$order->save(); 
  • Can i update it in this observer>> <?php namespace vendor_name\module_name\Observer\Sales;class OrderPlaceAfter implements \Magento\Framework\Event\ObserverInterface { /** * Execute observer * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute( \Magento\Framework\Event\Observer $observer ) { //Your observer code } } – Garry Nov 19 at 6:53
  • yes if the observer is working on order place after you can code it there as well – Asad Ullah Nov 19 at 6:56
  • if you can get any task done by plugin as well as observer in my knowledge plugin is good practice – Asad Ullah Nov 19 at 6:58
  • accept if answer helps – Asad Ullah Nov 19 at 7:07

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.