2

I am triggering sales_order_save_after event after order status has been completed.

public function execute(Observer $observer) {
    $order = $observer->getEvent()->getOrder();
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $checkoutSession=$objectManager->create('Magento\Checkout\Model\Session');
    $status = $checkoutSession->getLastOrderStatus();
    if( $status == "complete")
    { 
        echo "<script>javascript: alert ("message") </script>";
    }
}

and my event.xml file in Vendor/Namespace/etc/adminhtml/event.xml

I just want to show this popup when the order is completed in the admin panel of Magento 2 can anyone help to solve it. and how can I get to know my event is triggered or not.

  • Why you want to use alert instead of message manager ? I think it's best practise to use message manager. – Rohan Hapani Dec 6 '18 at 6:53
  • how to do that ? if you have any link can you just share? and i just want to confirm whether i am in the right path ! am i triggering the correct event? – ASHWINI MURTHY Dec 6 '18 at 6:57
  • Check my answer. – Rohan Hapani Dec 6 '18 at 7:17
  • Is it working for you or not? – Rohan Hapani Dec 6 '18 at 9:43
  • no Its not working!!! @rohan Hapani – ASHWINI MURTHY Dec 6 '18 at 10:00
2

Add this below code in your observer :

namespace Custom\Module\Observer;

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

class OrderStatusChange implements ObserverInterface {

    /**
     * @var \Magento\Framework\Message\ManagerInterface
     */
    protected $messageManager;
    /**
     * @var \Magento\Framework\App\Response\RedirectInterface
     */
    protected $redirect;
    public function __construct(
        ...
        \Magento\Framework\App\Response\RedirectInterface $redirect,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        ...
    ) {
        ... 
        $this->redirect = $redirect;
        $this->messageManager = $messageManager;
        ...
    }
    public function execute(Observer $observer) {
        $order = $observer->getEvent()->getOrder();
        $controller = $observer->getControllerAction();

        if($order->getState() == "complete")
        { 
            $this->messageManager->addSuccess(__('Order status change successfully')); // You can set your success message here
            $this->redirect->redirect($controller->getResponse(), $this->redirect->getRefererUrl()); // You can set here on which path you want to redirect
        }
    }
}

Remove generated folder and flush cache

| improve this answer | |

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.