cancel
Showing results for 
Search instead for 
Did you mean: 

On order Satus change event problem

SOLVED

On order Satus change event problem

Hey,

 

I managed to do a module and it is working as I can see, but I need an event that start on order status change. In this case I need that when the order goes to "processing" it does a call to an API and add a tracking number to the order.

I thought in using the sales_order_save_after event, check if the order as changed to "processing" and if so do the call and add the tracking number. Idk if there is a better way to do this.

But the problem is that I can't manage to get the order information, if I try to do the invoice in the backoffice it sais that the invoice can't be saved at the moment.

In the module with the event sales_order_save_after I just have

 

<?php

namespace Custom\onStatusChange\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;

class StatusChange implements ObserverInterface
{
    /**
     *
     * @param Observer $observer
     * @return $this
     */
    public function execute(Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();

        $incrementId = $order->getIncrementId();

        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info('Inc ID:');
        $logger->info($incrementId);

        return $this;
    }
}

Idk what's wrong.

Can someone help me with this?

 

Thanks Smiley Very Happy

1 ACCEPTED SOLUTION

Accepted Solutions

Re: On order Satus change event problem

I managed to get it to work like

 

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_save_after">
        <observer name="on_status_change" instance="Custom\onStatusChange\Observer\StatusChange"/>
    </event>
</config>

etc/module.xml

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

register.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Custom_onOrderCreate',
    __DIR__
);

Observer/StatusChange.php

<?php

namespace Custom\onStatusChange\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Message\ManagerInterface;

class StatusChange implements ObserverInterface
{
     /**
     * @var ObjectManagerInterface
     */
    protected $_objectManager;
    protected $messageManager;

    /**
     * @param \Magento\Framework\ObjectManagerInterface $objectManager
     */
    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager,
        \Magento\Quote\Model\QuoteFactory $quoteFactory,
        \Magento\Sales\Model\Order $order,
        ManagerInterface $messageManager
    )
    {
        $this->_objectManager = $objectManager;
        $this->messageManager = $messageManager;

    }


    public function execute(Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();
        
        if ($order instanceof \Magento\Framework\Model\AbstractModel) {
            if($order->getState() == 'processing') {
                Your code...
            } 
        }

    }
}

Probably most things on Observer/StatusChange.php are not needed but for now it is like this and runs when order it's saved, hope helps someone with problems creating this.

 

Thanks Smiley Very Happy

View solution in original post

3 REPLIES 3

Re: On order Satus change event problem

Hi @rui_silva1 ,

 

Can you please check and follow below link solution. Hope this helps you.

https://magento.stackexchange.com/questions/125058/whats-the-best-event-way-to-listen-for-all-order-...

 

Thanks!

------------

Problem solved? Click Accept as Solution!

Re: On order Satus change event problem

Thanks!

So in my files is missing the:

etc/di.xml

Model/Plugin/Pluginname.php ?

 

What should I do in the plugin file?

Just this?

<?php

namespace Custom\onStatusChange\Model\Plugin;


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

with the register.xml, etc/event.xml, etc/module.xml and the Observer/StatusChange.php?

 

What's the best way to test this?

 

Thanks! Smiley Very Happy

Re: On order Satus change event problem

I managed to get it to work like

 

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_save_after">
        <observer name="on_status_change" instance="Custom\onStatusChange\Observer\StatusChange"/>
    </event>
</config>

etc/module.xml

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

register.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Custom_onOrderCreate',
    __DIR__
);

Observer/StatusChange.php

<?php

namespace Custom\onStatusChange\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Message\ManagerInterface;

class StatusChange implements ObserverInterface
{
     /**
     * @var ObjectManagerInterface
     */
    protected $_objectManager;
    protected $messageManager;

    /**
     * @param \Magento\Framework\ObjectManagerInterface $objectManager
     */
    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager,
        \Magento\Quote\Model\QuoteFactory $quoteFactory,
        \Magento\Sales\Model\Order $order,
        ManagerInterface $messageManager
    )
    {
        $this->_objectManager = $objectManager;
        $this->messageManager = $messageManager;

    }


    public function execute(Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();
        
        if ($order instanceof \Magento\Framework\Model\AbstractModel) {
            if($order->getState() == 'processing') {
                Your code...
            } 
        }

    }
}

Probably most things on Observer/StatusChange.php are not needed but for now it is like this and runs when order it's saved, hope helps someone with problems creating this.

 

Thanks Smiley Very Happy