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

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);
    $this->responseFactory->create()->setRedirect($redirectUrl)->sendResponse();
    $observer->getControllerAction()->getResponse()->setRedirect($redirectUrl);
    return $this;
}
1
  • 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

0

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

2
  • 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

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.