<event name="sales_order_shipment_save_before">
    <observer name="test_beforeshipment" instance="Module\ModuleName\Observer\BeforeShipment" />

public function execute(\Magento\Framework\Event\Observer $observer)
    $shipment = $observer->getEvent()->getShipment();
    $order = $shipment->getOrder();
    $orderId = $order->getId();
    $redirectUrl = $this->url->getUrl('sales/order/view/order_id/'.$orderId);
    return $this;
  • Working fine the same code on the Magento2.4.2 version, but not working the same on the 2.4.3 Dec 16, 2021 at 9:40

1 Answer 1


If you want to do that then on __construct() function of your class observer, you must inject two classes.

First, \Magento\Framework\App\ResponseFactory which is responsible for redirection, Another class \Magento\Framework\UrlInterface will make a URL for that redirection. Then create object for ResponseFactory, and using setRedirect($YourUrl)->sendResponse(); redirect to your wished URL. Observer https://pastebin.com/4HwLUrth

  • Thanks for your kind reply, But I have already applied the solution that you have added but It is not working on the 2.4.3 Magento Version Dec 28, 2021 at 5:40
  • I am getting the issue in this line $observer->getControllerAction()->getResponse()->setRedirect($redirectUrl); Here getResponse() function returning null Dec 28, 2021 at 5:40
  • @AbhayAgrawal did you find a solution? I am facing the same error :)
    – Korbin
    Feb 10, 2023 at 11:00
  • @Korbin Please use this $error = "YOUR CUSTOM MESSAGE"; throw new \Magento\Framework\Validator\Exception(__($error)); Feb 13, 2023 at 5:12
  • @Korbin check the above solution and let me know the update Feb 13, 2023 at 5:13

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.