Can i save every order placed with custom payment method to order status complete?
-
20-03-2021 - |
Question
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.
Solution
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!
OTHER TIPS
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();