Using Observer stop execution of next function in Magento 2

I am using an observer on event sales_order_save_before to check for a condition and based on that condition I was to stop the save functionality.

Currently I am using it on Submit Comment in Sales Order view (Admin Panel)

My Observer execute function is called and condition is met. but it doesn’t stop the save functionality when I raise error using throw new ErrorException()

Is there any other way to completely stop the save order process?

Magento Asked by Nausif on October 2, 2021

1 Answers

One Answer

This is what I learnt from magento 2 certification exam : the data that is sent to events should not be transformed. Events should be able to be run completely asynchronously, so around plugin can be used to stop logic. For Submit Comment create aroundExecute() for MagentoSalesControllerAdminhtmlOrderAddComment

public function aroundExecute(AddComment $subject, Closure $proceed){
    //your statement;
    if (your statement) {
       // stop original chain
       redirect or throw Error;


Correct answer by Kai on October 2, 2021

Add your own answers!

Related Questions

How to Add Html Link Additional Options in Magento 2.3.3

1  Asked on November 30, 2020 by kanchi_1695


How to create a Grid View Slider. Magento 2

1  Asked on November 29, 2020 by asad-khan


Magento 2 Get all attributes option in phtml

3  Asked on November 29, 2020 by user1799722


magento 1.9, export data from grid to csv file

0  Asked on November 29, 2020 by denys-maksiura


magento admin login not working

3  Asked on November 28, 2020 by nitesh-kumar-singh


Magento 2.3 How to edit existing Order without cancel?

0  Asked on November 28, 2020 by rajesh-stanley


Magento bms/erp migration from to 2.3.4

0  Asked on November 18, 2020 by sahil-khanna


Magento2 Page jumps to top when click Menu Icon on mobile

1  Asked on November 13, 2020 by jaydip-patel


Ask a Question

Get help from others!

© 2021 All rights reserved.