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.

11
  • Why you want to use alert instead of message manager ? I think it's best practise to use message manager. Commented Dec 6, 2018 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? Commented Dec 6, 2018 at 6:57
  • Check my answer. Commented Dec 6, 2018 at 7:17
  • Is it working for you or not? Commented Dec 6, 2018 at 9:43
  • no Its not working!!! @rohan Hapani Commented Dec 6, 2018 at 10:00

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.